Laravel Eloquent多重搜索问题

时间:2018-03-29 13:19:55

标签: search laravel-5 laravel-eloquent

我有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);

我认为我的问题在哪里或者orWhere如果有多个参数,最后一次测试我将每个非空查询推送到$ conds数组,但我如何修复我的查询。

注意:user_data中使用的所有搜索参数。

此致

1 个答案:

答案 0 :(得分:0)

我将查询修改为:

$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);