mysql - Querying many to many relation laravel -


im trying query many many relationship laravel 5 eloquent,with pivot table, im trying retrieve "articles" has tag "a" , tag "b" together, not succeed, eloquents bring articles have tag "a" , tag"b", there`s way this? nothing bellows works.

$tags = [1,2,3];   $result = timetable::with([     'tags' => function($query) use($tags){         foreach($tags $tag) {             $query->where('id', $tag);         }     } ])->paginate();  $result = timetable::with(['tags'])                     ->wherehas('tags' => function($query) use($tags){                             $query->wherein('tag_id',$tags);                     }                 )->paginate(); 

assuming followed laravel's naming rules relationships , foreign keys:

get timetables have of tags

$result = timetable::with('tags')->wherehas('tags', function($query) use ($tags) {     $query->wherein('tag_id', $tags); })->paginate(); 

get timetables have of tags (no other tags accepted)

$result = timetable::with('tags')->wherehas('tags', function($query) use ($tags) {     $query->wherein('tag_id', $tags); }, '=', count($tags))->paginate(); 

get timetables have of tags may contain other tags

$result = timetable::with('tags')->wherehas('tags', function($query) use ($tags) {     $query->wherein('tag_id', $tags); }, '>=', count($tags))->paginate(); 

same above excluding results not matching tags

$result = timetable::with(['tags' => function($query) use ($tags) {     $query->wherein('tag_id', $tags); }])->wherehas('tags', function($query) use ($tags) {     $query->wherein('tag_id', $tags); }, '>=', count($tags))->paginate(); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -