php 如何在 Laravel 查询中使用多个 OR、AND 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43387955/
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
How to use multiple OR,AND condition in Laravel queries
提问by M Arfan
I need help for query in laravel
我需要帮助在 Laravel 中进行查询
My Custom Query: (Return Correct Result)
我的自定义查询:(返回正确的结果)
Select * FROM events WHERE status = 0 AND (type="public" or type = "private")
how to write this query in Laravel.
如何在Laravel 中编写此查询。
Event::where('status' , 0)->where("type" , "private")->orWhere('type' , "public")->get();
But it's returning all public events that status is not 0 as well.
但它也返回状态不为 0 的所有公共事件。
I am using Laravel 5.4
我正在使用 Laravel 5.4
回答by Alexey Mezenin
Pass closure into the where()
:
将闭包传递给where()
:
Event::where('status' , 0)
->where(function($q) {
$q->where('type', 'private')
->orWhere('type', 'public');
})
->get();
回答by Anar Bayramov
Use this
用这个
$event = Event::where('status' , 0);
$event = $event->where("type" , "private")->orWhere('type' , "public")->get();
or this
或这个
Event::where('status' , 0)
->where(function($result) {
$result->where("type" , "private")
->orWhere('type' , "public");
})
->get();
回答by Robert
In your case you can just rewrite the query...
在您的情况下,您可以重写查询...
select * FROM `events` WHERE `status` = 0 AND `type` IN ("public", "private");
And with Eloquent:
并与雄辩:
$events = Event::where('status', 0)
->whereIn('type', ['public', 'private'])
->get();
When you want to have a grouped OR/AND, use a closure:
当您想要分组 OR/AND 时,请使用闭包:
$events = Event::where('status', 0)
->where(function($query) {
$query->where('type', 'public')
->orWhere('type', 'private');
})->get();
回答by Milad.biniyaz
Try this. It works for me.
尝试这个。这个对我有用。
$rand_word=Session::get('rand_word');
$questions =DB::table('questions')
->where('word_id',$rand_word)
->where('lesson_id',$lesson_id)
->whereIn('type', ['sel_voice', 'listening'])
->get();