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, eloquent
s 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
Post a Comment