我试图显示一个没有评论的帖子列表。换句话说,在我一对多的关系中,如果有孩子,帖子就不应该显示。
除了使用原始查询进行此操作外,是否有一种简单的方法可以雄辩地做到这一点呢?
模型- Post.php
public function comments()
{
return $this->hasMany('Comment');
}模型- Comment.php
public function post()
{
return $this->belongsTo('Post');
}控制器- PostController.php
public function unanswered()
{
$posts = Post::with('comments')
->orderBy('created_at', 'desc')
->paginate(5);
return View::make('unanswered')->with('posts',$posts);
}发布于 2014-07-24 12:11:10
为了获取具有或不具有给定关系的模型,需要使用has方法。
根据您想要实现的目标,使用普通的has('relation')或传递其他参数:
$posts = Post::has('comments', '<', 1)->get();发布于 2014-07-24 12:04:29
使用(‘注释’)是急于加载
你需要的实际上是一个连接和一个where。
https://stackoverflow.com/questions/24933129
复制相似问题