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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -