我有两个模型Post和Comment,我想获取所有最后一条评论是活动状态的帖子:
我尝试了这个解决方案:
// Model Post
public function comments()
{
return $this->hasMany('comments');
}
//Model Comment
public function post()
{
return $this->belongsTo('post');
}
我尝试了这个解决方案:
public function lastComment()
{
return $this->hasOne('comment')->latest()
}
在我的控制器中:
$postsWithLastActiveComment = Post::whereHas('lastComment', function($q){
$q->where('active',1);
})->all();
但在这个解决方案中,如果最后一条评论不活跃,则会采取上一条评论。