Laravel: selecting with conditions from Many-to-many relationships
我在帖子和主题上有多对多的laravel关系:
- 帖子属于许多主题
- 主题属于许多职位
我想从某个主题获取ID> 10的帖子
以下代码将为我提供某些主题的所有帖子:
1 2 | $topic = Topic::where('id',$topic_id)->get()->first(); $posts= $topic->post; |
现在如何获取ID> 10的帖子?
型号:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Topic extends Eloquent{ public function post() { return $this->belongsToMany('post'); } } class Post extends Eloquent{ public function topic() { return $this->belongsToMany('Topic'); } } |
赞:
1 2 3 4 5 | Topic::with(array('posts' => function($q) { $q->where('id', '>', 10); }))->where('id', $id)->first(); |
您应该这样做
1 2 | $topic = Topic::find($topic_id); $posts= $topic->posts()->where('id','>',10)->get(); |
或
1 | $posts = Topic::find($topic_id)->posts()->where('id','>',10)->get(); |
希望获得帮助
如果要将
这里是条件
1 | $permissions = Role::with('getRolePermissions')->where('id', '=', Auth::guard('admin')->user()->id)->get()->toArray(); |
在此URL
1 2 3 4 | public function getRolePermissions() { return $this->belongsToMany('App\\Permission', 'permission_role'); } |