假设我有 posts
表有字段 minimum_comments
并且在 posts 表中有查询范围以使事件帖子执行类似的操作
public function scopeActive($query)
{
$query->has('comments', '>=', 'posts.minimum_comments');
}
考虑到所有关系已经建立
我怎样才能实现这个目标?
最佳答案
我还没有测试过,但试试这个:
// Post.php
public function scopeActive($query)
{
return $query->where('minimum_comments', '<=', $this->comments()->count());
}
然后你就可以像这样使用它:
// PostsController.php
public function myCoolFunction()
{
$posts = Post::active()->get();
// the rest of your logic..
}
关于mysql - Eloquent 获取关系计数等于列值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807770/