php - Laravel 5 – querying items by many-to-many related model -
i've got following many-to-many relation:
attendees - attendee_tag - tag
now want filter attendees based on tag value, i've created solution, works well:
$attendees = attendee::wherehas('tags', function ($q) { $q->where([ ['attendee_tag.tag_id', '=', '6'], ['attendee_tag.value_string', '=', 'summer ticket'] })->with(['tags' => function($q) { $q->select('tags.id', 'value_type', 'attendee_tag.value_string', 'attendee_tag.value_int'); }])->paginate($pagination);
but can't figure out, how filter attendees based on more 1 tag ... i've tried this, doesn't work:
$attendees = attendee::wherehas('tags', function ($q) { $q->where([ ['attendee_tag.tag_id', '=', '6'], ['attendee_tag.value_string', '=', 'summer ticket'] ])->where([ ['attendee_tag.tag_id', '=', '13'], ['attendee_tag.value_string', '=', 5] ]); })->with(['tags' => function($q) { $q->select('tags.id', 'value_type', 'attendee_tag.value_string', 'attendee_tag.value_int'); }])->paginate($pagination);
any highly appreciated! :)
maybe separating wherehas :
$attendees = attendee::wherehas('tags', function ($q0) { $q0->where([ ['attendee_tag.tag_id', '=', '6'], ['attendee_tag.value_string', '=', 'summer ticket'] ]); })->wherehas('tags', function ($q1) { $q1->where([ ['attendee_tag.tag_id', '=', '13'], ['attendee_tag.value_string', '=', 5] ]); })->with(['tags' => function($q) { $q->select('tags.id', 'value_type', 'attendee_tag.value_string', 'attendee_tag.value_int'); }])->paginate($pagination);
Comments
Post a Comment