mirror of
https://github.com/sp-tarkov/forge.git
synced 2025-02-12 20:20:41 -05:00
Minor User Model Changes
- Added a few docblock comments - Pint reordered methods (for some reason) - Added type casts for id, hub_id, created_at, and updated_at
This commit is contained in:
parent
b1195ebb7c
commit
c3f9fcf19f
@ -53,50 +53,64 @@ class User extends Authenticatable implements MustVerifyEmail
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The relationship between a user and users they follow
|
* The relationship between a user and users that follow them.
|
||||||
*/
|
*
|
||||||
public function following(): BelongsToMany
|
* @return BelongsToMany<User>
|
||||||
{
|
|
||||||
return $this->belongsToMany(User::class, 'user_follows', 'follower_id', 'following_id');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The relationship between a user and users that follow them
|
|
||||||
*/
|
*/
|
||||||
public function followers(): BelongsToMany
|
public function followers(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(User::class, 'user_follows', 'following_id', 'follower_id');
|
return $this->belongsToMany(User::class, 'user_follows', 'following_id', 'follower_id')
|
||||||
}
|
->withTimestamps();
|
||||||
|
|
||||||
public function isFollowing(User|int $user): bool
|
|
||||||
{
|
|
||||||
$userId = $user instanceof User ? $user->id : $user;
|
|
||||||
|
|
||||||
return $this->following()->where('following_id', $userId)->exists();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Follow another user.
|
||||||
|
*/
|
||||||
public function follow(User|int $user): void
|
public function follow(User|int $user): void
|
||||||
{
|
{
|
||||||
$userId = $user instanceof User ? $user->id : $user;
|
$userId = $user instanceof User ? $user->id : $user;
|
||||||
|
|
||||||
if ($this->id === $userId) {
|
if ($this->id === $userId) {
|
||||||
// don't allow following yourself
|
// Don't allow following yourself.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->following()->syncWithoutDetaching($userId);
|
$this->following()->syncWithoutDetaching($userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The relationship between a user and users they follow.
|
||||||
|
*
|
||||||
|
* @return BelongsToMany<User>
|
||||||
|
*/
|
||||||
|
public function following(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(User::class, 'user_follows', 'follower_id', 'following_id')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unfollow another user.
|
||||||
|
*/
|
||||||
public function unfollow(User|int $user): void
|
public function unfollow(User|int $user): void
|
||||||
{
|
{
|
||||||
$userId = $user instanceof User ? $user->id : $user;
|
$userId = $user instanceof User ? $user->id : $user;
|
||||||
|
|
||||||
// make sure the user is being followed before trying to detach
|
|
||||||
if ($this->isFollowing($userId)) {
|
if ($this->isFollowing($userId)) {
|
||||||
$this->following()->detach($userId);
|
$this->following()->detach($userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the user is following another user.
|
||||||
|
*/
|
||||||
|
public function isFollowing(User|int $user): bool
|
||||||
|
{
|
||||||
|
$userId = $user instanceof User ? $user->id : $user;
|
||||||
|
|
||||||
|
return $this->following()->where('following_id', $userId)->exists();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data that is searchable by Scout.
|
* The data that is searchable by Scout.
|
||||||
*/
|
*/
|
||||||
@ -209,8 +223,12 @@ class User extends Authenticatable implements MustVerifyEmail
|
|||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'id' => 'integer',
|
||||||
|
'hub_id' => 'integer',
|
||||||
'email_verified_at' => 'datetime',
|
'email_verified_at' => 'datetime',
|
||||||
'password' => 'hashed',
|
'password' => 'hashed',
|
||||||
|
'created_at' => 'datetime',
|
||||||
|
'updated_at' => 'datetime',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user