Update profile model, use proper follower relationship and add new methods

This commit is contained in:
Daniel Supernault 2018-05-31 16:03:31 -06:00
parent c8cb48a835
commit 21d7e47673

View file

@ -2,8 +2,9 @@
namespace App; namespace App;
use Illuminate\Database\Eloquent\Model;
use Storage; use Storage;
use App\Util\Lexer\PrettyNumber;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model class Profile extends Model
{ {
@ -15,7 +16,7 @@ class Profile extends Model
public function url($suffix = '') public function url($suffix = '')
{ {
return url('/@' . $this->username . $suffix); return url($this->username . $suffix);
} }
public function permalink($suffix = '') public function permalink($suffix = '')
@ -34,27 +35,63 @@ class Profile extends Model
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() public function following()
{ {
return $this->hasManyThrough( return $this->belongsToMany(
Profile::class, Profile::class,
Follower::class, 'followers',
'profile_id', 'profile_id',
'id', 'following_id'
'id',
'id'
); );
} }
public function followers() public function followers()
{ {
return $this->hasManyThrough( return $this->belongsToMany(
Profile::class, Profile::class,
Follower::class, 'followers',
'following_id', 'following_id',
'id', 'profile_id'
'id', );
'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'
); );
} }