2016-05-06 10 views
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をやっていると思っていましたが、どうやって分かりませんか?

+0

ユーザーが多くの投稿がありますし、ユーザーが多くの信者を持つことになります。

は、私はあなたがこのような何かをTOTワンだと思います。しかし、ユーザーと投稿者の「フォロー」を通じて関係を見る方法はありません。 – Abhishek

答えて

1

Hereは、回答と同様の質問へのリンクです。

User::where('id', $id)->with(['following.posts' => function ($q) use (&$posts) { 
    $posts= $q->get()->unique(); 
}])->first(); 
関連する問題