[英]How to restrict whereHas to ONLY match where condition?
I am using Laravel 6.0 and PostgreSQL on Homestead.我在 Homestead 上使用 Laravel 6.0 和 PostgreSQL。
This query is returning services which has at least one post with is_assistant = true
:此查询返回的服务至少有is_assistant = true
的帖子:
Service::with('posts')
->whereHas('posts', function ($q) {
$q->where('is_assistant', true);
})
->get();
Models:楷模:
class Service extends Model
{
public function posts()
{
return $this->belongsToMany('App\Post');
}
}
class Post extends Model
{
protected $casts = [
'is_assistant' => 'boolean',
];
}
How to write an Eloquent query that get services which has ONLY posts with is_assistant = true
?如何编写一个 Eloquent 查询来获取只有is_assistant = true
帖子的服务?
Can use the whereDoesntHave
method:可以使用whereDoesntHave
方法:
Service::with('posts')
->whereDoesntHave('posts', function ($q) {
$q->where('is_assistant', false);
})
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.