Laravel Eloquent 多重搜索问题

Laravel Eloquent issue with multiple search

我有 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.

中使用的所有搜索参数

此致,

我将查询修改为:

$clients = User::whereHas('roles', function ($query) {
                $query->where('name', '=', 'company');
            })->whereHas('userdata', function($query){
            $search_word    = (isset($_REQUEST['search']) && Input::has('search')) ? Input::get('search') : null;
            $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;

            if ($search_word != null){
                $query->where('id_number', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('address', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('website', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('telephone', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('fax', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('cr', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('zip_code', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('pobox', 'LIKE', '%' . $search_word .'%');
                $query->orWhere('description', 'LIKE', '%' . $search_word .'%');
            }
            if ($sector_id != null){
                $query->where('sector_id', $sector_id);
            }
            if ($sub_sectors != null){
                $query->where('sub_sectors', 'LIKE', '%' . $sub_sectors .'%');
            }
            if ($country_id != null){
                $query->where('country_id', $country_id);
            }
            if ($city_id != null){
                $query->where('city_id', $city_id);
            }
            if ($acc_type != null){
                if ($acc_type != 'all'){
                    $query->where('acc_type', $acc_type);
                }
            }

        })->paginate(25);