Laravel whereDoesntHave() - 多个 OR 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28065566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel whereDoesntHave() - multiple OR conditions
提问by KazikM
In Laravel 4.2 I have a model called Product with many-to-many relationshis to other models like Country or Category. I want to filter out products that are "incomplete", which means they have no connected countries or no connected categories. I can use whereDoesntHave()
method to filter out one relation. When I use it two times in one query it creates AND
condition, but I need OR
. I can't find orWhereDoesntHave()
method in API documentation. I can't pass multiple relations as arguments because it expects first argument to be a string.
在 Laravel 4.2 中,我有一个名为 Product 的模型,它与 Country 或 Category 等其他模型存在多对多关系。我想过滤掉“不完整”的产品,这意味着它们没有连接的国家或没有连接的类别。我可以使用whereDoesntHave()
方法过滤掉一种关系。当我在一个查询中使用它两次时,它会创建AND
条件,但我需要OR
. 我orWhereDoesntHave()
在 API 文档中找不到方法。我不能将多个关系作为参数传递,因为它期望第一个参数是一个字符串。
I need something like this:$products = Product::whereDoesntHave('categories')->orWhereDoesntHave('countries')->get();
我需要这样的东西:$products = Product::whereDoesntHave('categories')->orWhereDoesntHave('countries')->get();
Is there any way to achive whereDoesntHave()
with multiple OR
conditions?
有没有办法在whereDoesntHave()
多个OR
条件下实现?
回答by lukasgeiter
You can use doesntHave
and specify the boolean operator:
您可以使用doesntHave
并指定布尔运算符:
$products = Product::doesntHave('categories')->doesntHave('countries', 'or')->get();
Actually you only need whereDoesntHave
if you want to pass in a closure to filter the related models before checking if any of them exist. In case you want to do that you can pass the closure as third argument:
实际上,您只需要whereDoesntHave
在检查相关模型是否存在之前传入一个闭包来过滤相关模型。如果你想这样做,你可以将闭包作为第三个参数传递:
$products = Product::doesntHave('categories', 'or', function($q){
$q->where('active', false);
})->doesntHave('countries', 'or')->get();
回答by Adam
Since Laravel 5.5 there is a orWhereDoesntHavefunction.
从 Laravel 5.5 开始,有一个orWhereDoesntHave函数。
You may use it like this
你可以这样使用它
Product::Product::whereDoesntHave('categories', function($q){ //... })
->orWhereDoesntHave('countries', function($q){//...})
->get();
From you example it seems that you are not using a where clause, so you may just use
从你的例子来看,你似乎没有使用 where 子句,所以你可以使用
Product::Product::doesntHave('categories')
->ordoesntHave('countries')
->get();
回答by Jan P.
Use
用
Product::whereDoesntHave('categories')->doesntHave('countries', 'or')->get();
Laravel Source Code:
Laravel 源代码:
whereDoesntHave https://github.com/illuminate/database/blob/master/Eloquent/Builder.php#L654calls https://github.com/illuminate/database/blob/master/Eloquent/Builder.php#L628internally.
whereDoesntHave https://github.com/illuminate/database/blob/master/Eloquent/Builder.php#L654调用 https://github.com/illuminate/database/blob/master/Eloquent/Builder.php#L628内部。