【问题标题】:Laravel Eloquent issue with multiple searchLaravel Eloquent 多次搜索问题
【发布时间】: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


【解决方案1】:

我将查询固定为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 2013-06-19
    • 2018-07-23
    • 2021-05-16
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2014-05-24
    相关资源
    最近更新 更多