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:
Refringe 2024-09-17 15:33:42 -04:00
parent b1195ebb7c
commit c3f9fcf19f
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k

View File

@ -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',
]; ];
} }
} }