|
10 | 10 | use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
11 | 11 | use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
12 | 12 |
|
| 13 | +use Auth; |
| 14 | + |
13 | 15 | class User extends Model implements AuthenticatableContract,
|
14 | 16 | AuthorizableContract,
|
15 | 17 | CanResetPasswordContract
|
@@ -64,7 +66,41 @@ public function statuses()
|
64 | 66 |
|
65 | 67 | public function feed()
|
66 | 68 | {
|
67 |
| - return $this->statuses() |
68 |
| - ->orderBy('created_at', 'desc'); |
| 69 | + $user_ids = Auth::user()->followings->pluck('id')->toArray(); |
| 70 | + array_push($user_ids, Auth::user()->id); |
| 71 | + return Status::whereIn('user_id', $user_ids) |
| 72 | + ->with('user') |
| 73 | + ->orderBy('created_at', 'desc'); |
| 74 | + } |
| 75 | + |
| 76 | + public function followers() |
| 77 | + { |
| 78 | + return $this->belongsToMany(User::Class, 'followers', 'user_id', 'follower_id'); |
| 79 | + } |
| 80 | + |
| 81 | + public function followings() |
| 82 | + { |
| 83 | + return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id'); |
| 84 | + } |
| 85 | + |
| 86 | + public function follow($user_ids) |
| 87 | + { |
| 88 | + if (!is_array($user_ids)) { |
| 89 | + $user_ids = compact('user_ids'); |
| 90 | + } |
| 91 | + $this->followings()->sync($user_ids, false); |
| 92 | + } |
| 93 | + |
| 94 | + public function unfollow($user_ids) |
| 95 | + { |
| 96 | + if (!is_array($user_ids)) { |
| 97 | + $user_ids = compact('user_ids'); |
| 98 | + } |
| 99 | + $this->followings()->detach($user_ids); |
| 100 | + } |
| 101 | + |
| 102 | + public function isFollowing($user_id) |
| 103 | + { |
| 104 | + return $this->followings->contains($user_id); |
69 | 105 | }
|
70 | 106 | }
|
0 commit comments