実現したいこと
Laravelで、ブログ投稿アプリを製作しています。
そのアプリで、ログインユーザーがフォローしているユーザーの投稿を取得したいと思っております。(Twitterでいうタイムラインを実装するため。
試したこと
class UserController extends Controller {
public function timeline()
{
$posts = Post::where('user_id', ココがわからない)->get();
}
}
上記の第二引数に適切な値を記述すれば、取得できると考えているのですが、その値の検討がつきません。
第二引数にどのような値を記述すれば良いでしょうか?
また、別のアプローチなどがあればご教授していただけると幸いです。
補足情報
フォローに関するテーブルは以下になります。
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| follower_id | bigint(20) unsigned | NO | MUL | NULL | |
| followee_id | bigint(20) unsigned | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
リレーションの記述は以下になります。
User.php
public function posts()
{
return $this->hasMany('App\Post');
}
public function followings(): BelongsToMany
{
return $this->belongsToMany('App\User', 'follows', 'follower_id', 'followee_id')->withTimestamps();
}
public function followers(): BelongsToMany
{
return $this->belongsToMany('App\User', 'follows', 'followee_id', 'follower_id')->withTimestamps();
}
参考資料などが足りない場合などは、お申しつけください。
お手数おかけしますが、ご教授していただけると幸いです。