gpt4 book ai didi

laravel - 如何在 laravel 查询中添加括号?

转载 作者:行者123 更新时间:2023-12-01 13:47:45 25 4
gpt4 key购买 nike

我的查询 laravel 是这样的:

$customer = Customer::where('full_name', 'iLIKE', '%'.$param['keyword'].'%')
->orWhere('mobile', 'iLIKE', '%'.$param['keyword'].'%')
->orWhere('phone', 'iLIKE', '%'.$param['keyword'].'%')
->where('published','1')
->where('customer_status','N')
->orderBy('full_name', 'ASC')->get();

如果我打印常规查询使用这个:

DB::listen(function($sql, $bindings, $time) {
\Log::info($sql);
\Log::info(print_r($bindings,1));
\Log::info($time);
});

结果:

select * from "ss_customer" 
where "full_name" iLIKE '%ronaldo%' or "mobile" iLIKE '%ronaldo%' or "phone" iLIKE '%ronaldo%' and "published" = 1 and "customer_status" = 'N'
order by "full_name" asc

我想添加方括号,这样查询就变成这样:

select * from "ss_customer" 
where ("full_name" iLIKE '%ronaldo%' or "mobile" iLIKE '%ronaldo%' or "phone" iLIKE '%ronaldo%') and "published" = 1 and "customer_status" = 'N'
order by "full_name" asc

如何在 laravel 查询中添加括号?

谢谢

最佳答案

这很简单,只需将您的查询作为闭包传递即可

       $customer = Customer::where(function($query) use ($param) {
$query->where('full_name', 'iLIKE', '%'.$param['keyword'].'%')
->orWhere('mobile', 'iLIKE', '%'.$param['keyword'].'%')
->orWhere('phone', 'iLIKE', '%'.$param['keyword'].'%');
})->where('published','1')
->where('customer_status','N')
->orderBy('full_name', 'ASC')->get();

关于laravel - 如何在 laravel 查询中添加括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34589028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com