【发布时间】:2018-09-08 11:26:15
【问题描述】:
我有 2 个表(用户,user_data),我正在尝试使用多个 GET 方法进行搜索,例如:
http://xxx.loc/clients?sector_id=1&country_id=1&city_id=2&acc_type=all
如果没有参数,我的代码就可以工作。
这是我的查询代码:
$clients = User::whereHas('roles', function ($query) {
$query->where('name', '=', 'company');
})->with(['userdata' => function($query) {
$sector_id = (isset($_REQUEST['sector_id']) && Input::has('sector_id')) ? Input::get('sector_id') : null;
$sub_sectors = (isset($_REQUEST['sub_sectors']) && Input::has('sub_sectors')) ? Input::get('sub_sectors') : null;
$country_id = (isset($_REQUEST['country_id']) && Input::has('country_id')) ? Input::get('country_id') : null;
$city_id = (isset($_REQUEST['city_id']) && Input::has('city_id')) ? Input::get('city_id') : null;
$acc_type = (isset($_REQUEST['acc_type']) && Input::has('acc_type')) ? Input::get('acc_type') : null;
$conds = [];
if ($sector_id != null){
$conds[] = $query->where('sector_id', $sector_id);
}
if ($sub_sectors != null){
$conds[] = $query->whereIn('sub_sectors', $sub_sectors);
}
if ($country_id != null){
$conds[] = $query->where('country_id', $country_id);
}
if ($city_id != null){
$conds[] = $query->where('city_id', $city_id);
}
if ($acc_type != null){
if ($acc_type != 'all'){
$conds[] = $query->where('acc_type', $acc_type);
}
}
dd($sector_id, $sub_sectors, $country_id, $city_id, $acc_type, $conds);
}])->paginate(25);
如果有多个参数,我认为我的 where 或 orWhere 存在问题,最后一次测试我将每个非空查询推送到 $conds 数组,但是我该如何修复我的查询。
注意:user_data 中使用的所有搜索参数。
问候,
【问题讨论】:
-
现在的结果是什么,预期的结果是什么?
标签: search laravel-5 laravel-eloquent