Ila*_*kel 1 php laravel eloquent laravel-5 laravel-eloquent
我正在对我的数据进行搜索查询,我只想获取列中值为 0 的可用行,is_temp如果用户向我发送搜索查询,我想在其他列中搜索此值
这就是我现在所拥有的:
$clients = Company::where('guid',$guid)->first()
->clients()
->where('is_temp',0)
->orderBy('name', $sort_order)
->skip($per_page*($page-1))
->take($per_page);
if($search_query != ""){
$clients = $clients->orWhere('name','LIKE','%'.$search_query.'%')
->orWhere('email','LIKE','%'.$search_query.'%')
->orWhere('vat_number','LIKE','%'.$search_query.'%')
->orWhere('contact_name','LIKE','%'.$search_query.'%');
}
$response = [
'clients' => $clients->get()
];
return response()->json($response, 200);
Run Code Online (Sandbox Code Playgroud)
但是当我以这种方式使用它时,我也会得到字段is_temp等于 1的行,所以我需要使用 (where) 和 (where 或 where 或 where...)
我怎么做?
您需要将orWhere调用组合在一起。以下应该可以解决问题:
if($search_query != ""){
$clients = $clients->where(function($query) use ($search_query) {
$query->where('name','LIKE','%'.$search_query.'%')
->orWhere('email','LIKE','%'.$search_query.'%')
->orWhere('vat_number','LIKE','%'.$search_query.'%')
->orWhere('contact_name','LIKE','%'.$search_query.'%');
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7052 次 |
| 最近记录: |