Elm*_*sry 3 many-to-many laravel
对于帖子和主题,我有一个多对多的 Laravel 关系:
我想从某个主题中获取id > 10 的帖子
以下代码将为我获取某个主题的所有帖子:
$topic = Topic::where('id',$topic_id)->get()->first();
$posts= $topic->post;
Run Code Online (Sandbox Code Playgroud)
现在如何获取id > 10 的帖子 ?
楷模:
class Topic extends Eloquent{
public function post()
{
return $this->belongsToMany('post');
}
}
class Post extends Eloquent{
public function topic()
{
return $this->belongsToMany('Topic');
}
}
Run Code Online (Sandbox Code Playgroud)
像这样:
Topic::with(array('posts' => function($q)
{
$q->where('id', '>', 10);
}))->where('id', $id)->first();
Run Code Online (Sandbox Code Playgroud)