2
私はUser
モデル、Post
モデルを持っています。私はEloquentに従っている人の投稿を得る方法
public function followers()
{
return $this->belongsToMany(User::class, 'followers', 'follower_id', 'user_id')->withTimestamps();
}
public function following()
{
return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id')->withTimestamps();
}
public function posts()
{
return $this->hasMany(Post::class);
}
だから基本的には、$user->following
は私に、私は、次の午前ユーザーのコレクションを与え、$user->posts
は私の記事のコレクションを与える:私のUser
モデルで
は、私は次のよう持っています。私はこれをしたい:
$posts = [];
$user->following->each(function($f) use($posts) {
$posts[] = $f->posts;
});
しかし、それは理にかなっています。私はこの後に結果を改ページしたいので。私はhasManyThrough
をやっていると思っていましたが、どうやって分かりませんか?
ユーザーが多くの投稿がありますし、ユーザーが多くの信者を持つことになります。
は、私はあなたがこのような何かをTOTワンだと思います。しかし、ユーザーと投稿者の「フォロー」を通じて関係を見る方法はありません。 – Abhishek