From c3f9fcf19fc7e4bedd08010f31dc2e875dafa431 Mon Sep 17 00:00:00 2001 From: Refringe Date: Tue, 17 Sep 2024 15:33:42 -0400 Subject: [PATCH] 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 --- app/Models/User.php | 56 ++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 8df99dd..3fb00db 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -53,50 +53,64 @@ class User extends Authenticatable implements MustVerifyEmail } /** - * The relationship between a user and users they follow - */ - public function following(): BelongsToMany - { - return $this->belongsToMany(User::class, 'user_follows', 'follower_id', 'following_id'); - } - - /** - * The relationship between a user and users that follow them + * The relationship between a user and users that follow them. + * + * @return BelongsToMany */ public function followers(): BelongsToMany { - return $this->belongsToMany(User::class, 'user_follows', 'following_id', 'follower_id'); - } - - public function isFollowing(User|int $user): bool - { - $userId = $user instanceof User ? $user->id : $user; - - return $this->following()->where('following_id', $userId)->exists(); + return $this->belongsToMany(User::class, 'user_follows', 'following_id', 'follower_id') + ->withTimestamps(); } + /** + * Follow another user. + */ public function follow(User|int $user): void { $userId = $user instanceof User ? $user->id : $user; if ($this->id === $userId) { - // don't allow following yourself + // Don't allow following yourself. return; } $this->following()->syncWithoutDetaching($userId); } + /** + * The relationship between a user and users they follow. + * + * @return BelongsToMany + */ + 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 { $userId = $user instanceof User ? $user->id : $user; - // make sure the user is being followed before trying to detach if ($this->isFollowing($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. */ @@ -209,8 +223,12 @@ class User extends Authenticatable implements MustVerifyEmail protected function casts(): array { return [ + 'id' => 'integer', + 'hub_id' => 'integer', 'email_verified_at' => 'datetime', 'password' => 'hashed', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', ]; } }