From 21d7e47673027bde1532225a84e9c956dc1e3e2f Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 31 May 2018 16:03:31 -0600 Subject: [PATCH] Update profile model, use proper follower relationship and add new methods --- app/Profile.php | 77 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/app/Profile.php b/app/Profile.php index dbd666c9d..3c4b27cea 100644 --- a/app/Profile.php +++ b/app/Profile.php @@ -2,8 +2,9 @@ namespace App; -use Illuminate\Database\Eloquent\Model; use Storage; +use App\Util\Lexer\PrettyNumber; +use Illuminate\Database\Eloquent\Model; class Profile extends Model { @@ -15,14 +16,14 @@ class Profile extends Model public function url($suffix = '') { - return url('/@' . $this->username . $suffix); + return url($this->username . $suffix); } public function permalink($suffix = '') { return url('users/' . $this->username . $suffix); } - + public function emailUrl() { $domain = parse_url(config('app.url'), PHP_URL_HOST); @@ -31,31 +32,67 @@ class Profile extends Model public function statuses() { - return $this->hasMany(Status::class); + return $this->hasMany(Status::class); + } + + public function followingCount($short = false) + { + $count = $this->following()->count(); + if($short) { + return PrettyNumber::convert($count); + } else { + return $count; + } + } + + public function followerCount($short = false) + { + $count = $this->followers()->count(); + if($short) { + return PrettyNumber::convert($count); + } else { + return $count; + } } public function following() { - return $this->hasManyThrough( - Profile::class, - Follower::class, - 'profile_id', - 'id', - 'id', - 'id' - ); + return $this->belongsToMany( + Profile::class, + 'followers', + 'profile_id', + 'following_id' + ); } public function followers() { - return $this->hasManyThrough( - Profile::class, - Follower::class, - 'following_id', - 'id', - 'id', - 'id' - ); + return $this->belongsToMany( + Profile::class, + 'followers', + 'following_id', + 'profile_id' + ); + } + + public function follows($profile) + { + return Follower::whereProfileId($this->id)->whereFollowingId($profile->id)->count(); + } + + public function followedBy($profile) + { + return Follower::whereProfileId($profile->id)->whereFollowingId($this->id)->count(); + } + + public function bookmarks() + { + return $this->belongsToMany( + Status::class, + 'bookmarks', + 'profile_id', + 'status_id' + ); } public function likes()