Laravel Eloquent - (where) and (where or where or where)
Laravel Eloquent - (where) and (where or where or where)
我正在为我的数据进行搜索查询,
我只想获取可用行,即 is_temp
列中值为 0 的行,如果用户向我发送搜索查询,我想在其他列中搜索该值
这是我现在拥有的:
$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);
但是当我以这种方式使用它时,我也会得到字段 is_temp
等于 1 的行
所以我需要使用 (where) 和 (where or where or 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.'%');
});
}
为 orWhere 使用组:
if($search_query != ""){
$clients = Company::where('guid',$guid)->first()
->where('is_temp',0)
->where(function($query) {
$query->where('name','LIKE','%'.$search_query.'%')
->orWhere('email','LIKE','%'.$search_query.'%')
->orWhere('vat_number','LIKE','%'.$search_query.'%')
->orWhere('contact_name','LIKE','%'.$search_query.'%')
})->orderBy('name', $sort_order)
->skip($per_page*($page-1))
->take($per_page);
}
$response = [
'clients' => $clients->get()
];
return response()->json($response, 200);
我正在为我的数据进行搜索查询,
我只想获取可用行,即 is_temp
列中值为 0 的行,如果用户向我发送搜索查询,我想在其他列中搜索该值
这是我现在拥有的:
$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);
但是当我以这种方式使用它时,我也会得到字段 is_temp
等于 1 的行
所以我需要使用 (where) 和 (where or where or 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.'%');
});
}
为 orWhere 使用组:
if($search_query != ""){
$clients = Company::where('guid',$guid)->first()
->where('is_temp',0)
->where(function($query) {
$query->where('name','LIKE','%'.$search_query.'%')
->orWhere('email','LIKE','%'.$search_query.'%')
->orWhere('vat_number','LIKE','%'.$search_query.'%')
->orWhere('contact_name','LIKE','%'.$search_query.'%')
})->orderBy('name', $sort_order)
->skip($per_page*($page-1))
->take($per_page);
}
$response = [
'clients' => $clients->get()
];
return response()->json($response, 200);