Laravel 雄辩多个 WHERE 与 OR AND OR 和 LIKE?

问题描述 投票:0回答:6

我在 Laravel 中有(或正在尝试进行)此查询

我遇到的问题是

$orThose

我想询问
LIKE

该领域的数据可以有很多,但该领域总会有一个像“L3”或“ICU”这样的关键工作。

在这种情况下您可以进行

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();
laravel eloquent
6个回答
62
投票

like
not like
方法中将
%
where()
orWhere()
一起使用:

->where('column', 'like', '%pattern%')

https://laravel.com/docs/5.3/queries#where-clauses

如果您需要使用多个

AND
,请这样做:

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

如果您想使用

OR
,请执行以下操作:

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

要组合多个

AND
OR
进行参数分组:

->where('name', '=', 'John')
->orWhere(function ($query) {
    $query->where('votes', '>', 100)
          ->where('title', '<>', 'Admin');
})

17
投票

在这种情况下参数分组使用多个ANDOR的组合应该非常有用。以下是一些如何实现的演示示例。

if ($request->search) {
             
     $users = User::where('type', '=',  'Customer')
        ->where(function ($query) use ($request) {
            $query->where('name', "like", "%" . $request->search . "%");
            $query->orWhere('mobile', "like", "%" . $request->search . "%");
        })->get();   
  }

它描绘了:

select * from users where type = 'Customer' and ([name as searched] or [mobile as searched])

现在还有一个例子:

DB::table('users')
            ->where('name', '=', 'Rohan')
            ->orWhere(function ($query) {
                $query->where('votes', '>', 100)
                      ->where('title', '<>', 'Admin');
            })->get();

它描绘了:

select * from users where name = 'Rohan' or (votes > 100 and title <> 'Admin')

我希望在这些的帮助下你可以解决这个问题。


10
投票

嗨,请查找带有“or”和“and”的 where 条件的参考

表库存

$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);

7
投票
$matchThese = ['title' => post('title'), 'slug' => post('slug')];
return Blog::where($matchThese)->get();

3
投票
public function searchData(Request $request)
{
  return Phonebook::where('name', 'like', '%'.$request->SearchQuery.'%')->orWhere('email', 'like', '%'.$request->SearchQuery.'%')->orWhere('phone', 'like', '%'.$request->SearchQuery.'%')->get();
}

在这里,我使用 whereorwherelike

从电话簿模型中搜索了姓名、电子邮件和电话号码

0
投票

此代码适用于最新的 Laravel

->whereAny([
   'model_name',
   'serial_number'
], 'LIKE', '%search_string%');

来源 - https://laravel.com/docs/11.x/queries#where-any-all-clauses

© www.soinside.com 2019 - 2024. All rights reserved.