Laravel:从多对多关系中选择条件 [英] Laravel: selecting with conditions from Many-to-many relationships
本文介绍了Laravel:从多对多关系中选择条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在帖子和主题上有很多对等的关系:
I have a many-to-many laravel relationship for posts and topics:
- 帖子属于许多主题
- 主题属于许多帖子
我想从某个主题
以下代码将带我某些主题的所有帖子:
The following code will get me all the posts from certain topic:
$topic = Topic::where('id',$topic_id)->get()->first();
$posts= $topic->post;
现在如何获取 id> 10 的帖子?
型号:
class Topic extends Eloquent{
public function post()
{
return $this->belongsToMany('post');
}
}
class Post extends Eloquent{
public function topic()
{
return $this->belongsToMany('Topic');
}
}
推荐答案
像这样:
Topic::with(array('posts' => function($q)
{
$q->where('id', '>', 10);
}))->where('id', $id)->first();
这篇关于Laravel:从多对多关系中选择条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文