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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:31:39  来源:igfitidea点击:

Laravel eloquent multiple WHERE with OR AND OR and LIKE?

laraveleloquent

提问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 LIKEquery 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 likeand not likewith %in where()and orWhere()methods:

使用likeand not likewith %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 ANDdo like this:

如果您需要使用多个AND这样做:

->where($condition)
->where($anotherCondition)

If you want to use ORdo this:

如果你想使用OR这样做:

->where($condition)
->orWhere($anotherCondition)

To combine multiple ANDand ORdo parameter grouping:

要组合多个ANDOR进行参数分组

->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

餐桌库存

enter image description here

在此处输入图片说明

$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