Laravel 用 OR AND OR 和 LIKE 雄辩多个 WHERE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39594588/
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 eloquent multiple WHERE with OR AND OR and LIKE?
提问by morne
I have ( or am trying to make ) this query in Laravel
我在 Laravel 中有(或正在尝试进行)此查询
The problem I have is with the $orThose
I want to query LIKE
.
我遇到的问题是$orThose
I want to query LIKE
。
The data in that field can be many things, but there will always be a key work like "L3" or "ICU" in the field.
该领域的数据可以是很多东西,但该领域中总会有像“L3”或“ICU”这样的关键工作。
Can you do a LIKE
query in this case
LIKE
在这种情况下你能做一个查询吗
$matchThese = ['link.hos_id' => $hos_id, 'outcome.otc_otrdischargedate' => $td];
$orThose = ['outcome.otc_outcome' => '@ICU@', 'outcome.otc_outcome' => '@I.C.U@'];
$todaysReferrals = DB::table('link')
->join('daily_link', 'link.lnk_id', '=', 'daily_link.dlk_lnkid')
->join('demographic', 'link.lnk_dmgid', '=', 'demographic.dmg_id')
->join('admission', 'link.lnk_admid', '=', 'admission.adm_id')
->join('or_call', 'admission.adm_calid', '=', 'or_call.cal_id')
->join('admission_score', 'admission.adm_scoreid', '=', 'admission_score.ascore_id')
->join('diagnosis', 'link.lnk_dgnid', '=', 'diagnosis.dgn_id')
->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id')
->where($matchThese)
->orWhere($orThose)
->get();
回答by Alexey Mezenin
Use like
and not like
with %
in where()
and orWhere()
methods:
使用like
and not like
with %
inwhere()
和orWhere()
方法:
->where('column', 'like', '%pattern%')
https://laravel.com/docs/5.3/queries#where-clauses
https://laravel.com/docs/5.3/queries#where-clauses
If you need to use multiple AND
do like this:
如果您需要使用多个AND
这样做:
->where($condition)
->where($anotherCondition)
If you want to use OR
do this:
如果你想使用OR
这样做:
->where($condition)
->orWhere($anotherCondition)
To combine multiple AND
and OR
do parameter grouping:
要组合多个AND
并OR
进行参数分组:
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
回答by Krishnamoorthy Acharya
Hi Please find the reference for where condition both with 'or' and 'and'
嗨,请找到有关“或”和“和”条件的参考
Table stocks
餐桌库存
$name='appl';
$type=1;
$stocks = DB::table('stocks')
->where('name', 'like', '%' . $name . '%')
->orWhere('company_name', 'like', '%' . $name . '%')
->where('exchange_id', '=', $type)
->get();
$results = json_decode($stocks, true);
print_r($results);
回答by Andino Inyang
$matchThese = ['title' => post('title'), 'slug' => post('slug')];
return Blog::where($matchThese)->get();
回答by Anandan K
public function searchData(Request $request)
{
return Phonebook::where('name', 'like', '%'.$request->SearchQuery.'%')->orWhere('email', 'like', '%'.$request->SearchQuery.'%')->orWhere('phone', 'like', '%'.$request->SearchQuery.'%')->get();
}
Here I have Searched the name, email and phone number from Phonebook Model using where, orwhereand like
在这里,我搜索的从电话簿型号使用的姓名,电子邮件和电话号码,其中,orwhere和像