使用 Eloquent ORM,我的模型设置如下:Post
belongsToMany
Category
Post.php
public function categories()
{
return $this->belongsToMany('Category', 'posts_categories');
}
我想按类别关系的列过滤帖子。
所以我想做一些类似的事情:
$posts->where('categories.slug', '=', Input::get('category_slug'));
但这不起作用。
我也尝试过:
$with['categories'] = function($query){
$query->where('slug', '=', Input::get('category_slug'));
};
$posts::with($with)->get();
但我认为这是为了过滤类别而不是按类别过滤。
谁能给我指路吗?
我现在无法访问我的 Vagrant 盒子,但我相信这应该可行:
$posts = Post::whereHas('categories', function($q)
{
$q->where('slug', '=', Input::get('category_slug'));
})->get();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)