jeudi 29 mars 2018

Laravel Eloquent issue with multiple search

I've 2 tables (users, user_data), I'm try to make search with multiple GET method like:

http://xxx.loc/clients?sector_id=1&country_id=1&city_id=2&acc_type=all

if there isn't parameters my code is work.

here is my query code:

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

I think my problem with where or orWhere if there are multiple parameters, last test i pushed each not null query to $conds array, but how i can fix my query.

note: all search parameters used in user_data.

Regards,



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire