gpt4 book ai didi

laravel - 如何限制 whereHas 只匹配 where 条件?

转载 作者:行者123 更新时间:2023-11-29 14:25:49 24 4
gpt4 key购买 nike

我在 Homestead 上使用 Laravel 6.0 和 PostgreSQL。

此查询返回的服务至少有一个带有is_assistant = true的帖子:

Service::with('posts')
->whereHas('posts', function ($q) {
$q->where('is_assistant', true);
})
->get();

模型:

class Service extends Model
{
public function posts()
{
return $this->belongsToMany('App\Post');
}
}

class Post extends Model
{

protected $casts = [
'is_assistant' => 'boolean',
];

}

如何编写 Eloquent 查询以获取只有 is_assistant = true 帖子的服务?

最佳答案

可以使用whereDoesntHave方法:

Service::with('posts')
->whereDoesntHave('posts', function ($q) {
$q->where('is_assistant', false);
})
->get();

关于laravel - 如何限制 whereHas 只匹配 where 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58173585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com