php - Laravel multiple table join saved to a variable and running a foreach against it (search function) -


i presently have 3 tables: shows, genres , show_genre (associates two). have search form submits series of checkboxes values array based on genres selected. presently want associate shows table , genres table variable , run query against once every genre checkbox selected. then, once selection filtered, can display resulting show objects matched users parameters.

my present setup following

public function searchshows(searchrequest $request) {     //$showswithgenres give collection inside collection can't seem access values without doing bunch of ugly code using count() in loop     $showswithgenres = show::with('genres')->get();     $genres = $request->name;     if(isset($genres))     {         foreach($genres $genre)         {             //iterate against selection repeatedly reducing number of results         }     } } 

thanks.

you should use wherehas() , wherein.

perhaps should it:

$shows = show::wherehas('genres', function($q) use($genre_ids) {     $q->wherein('id', $genre_ids);  })->get(); 

edit try this, i'm unsure performance.

$query= show::query();  foreach($genre_ids $id){     $query->wherehas('genres', function($q) use($id)         {             $q->where('id', $id);         }) } $shows = $query->get(); 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -