php - Query builder conditional parameters -


i have user table has related data on belongstomany basis

 users     id     first_name   skills     id     name   positions     id     name   position_user     position_id     user_id     created_at     updated_at   skill_user    skill_id    user_id    created_at    updated_at 

in user model

public function positions() {     return $this->belongstomany('app\position')->withtimestamps(); } 

and in position

public function users() {     return $this->belongstomany('app\user')->withtimestamps(); } 

(the same skills)

i passing following view:

$users = user::with('skills') ->with('skills') ->with('positions') ->get(); 

i want able search on various combinations of skills , positions having difficulty creating elegant solution.

if select position or positions, pass controller can return info as:

if (request::get('positions')) {     $positions = request::get('positions'); } 

where positions array array:3 [? 0 => "analyst" 1 => "attorney" 2 => "architect"]

if($positions) {     $users = user::with('skills')     ->with('skills')     ->with('positions')     ->orwherehas('positions', function($thisquery) use ($positions)     {         $thisquery->wherein('name', $positions);     })     ->get(); } 

if same skills works well.

what need combine them - since adding more related tables search function.

i have tried:

if(($positions)&&($skills)) {     $users = user::with('skills')     ->with('skills')     ->with('positions')     ->orwherehas('positions', function($thisquery) use ($positions)     {        $thisquery->wherein('name', $positions);     })     ->orwherehas('skills', function($thisquery) use ($skills)     {         $thisquery->wherein('name', $skills);     })     ->get(); } 

but wanting more like

$users = user::with('skills') ->with('skills') ->with('positions'); if($skills) {     $users->orwherehas('skills', function($thisquery) use ($skills)     {        $thisquery->wherein('name', $skills);     });  }  if($positions) {     $users->orwherehas('positions', function($thisquery) use ($positions)     {          $thisquery->wherein('name', $positions);     }); } $users->get(); 

however doesn't work -just returns empty resultset.

how can achieve in elegant way?

or there perhaps better way implement search function - wanting filter on parameters.

i think need "and where" condition here. or means matching either 1 or or both rules. "and where" work if both rules match.

so i'd suggest trying this:

$users = user::with('skills') ->with('skills') ->with('positions'); if($skills) {     $users->wherehas('skills', function($thisquery) use ($skills)     {        $thisquery->wherein('name', $skills);     });  }  if($positions) {     $users->wherehas('positions', function($thisquery) use ($positions)     {          $thisquery->wherein('name', $positions);     }); } $users = $users->get(); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -