W
W
WebDev2016-07-27 18:45:40
Laravel
WebDev, 2016-07-27 18:45:40

Laravel correct whereHas?

Hello, there is a database of entities groups, they have tags, you need to make a selection of all entities that either have no tags at all or have tags, except for the excluded ones

$entities = Entity::whereHas('tags', function($query) use ($excluded) {
    $query->whereNotIn('tags.id', $excluded);
});

Here it turns out that we take entities that have tags in addition to those excluded, but if the entity has no tags at all, then they also need to be selected. Can this be done without join?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2016-07-27
@Sanasol

orWhereIsNull can be added

A
Andrzej Wielski, 2016-07-29
@wielski

$entities = Entity::where(function($q) use ($excluded){
    $q->whereHas('tags', function($query) use ($excluded) {
        $query->whereNotIn('tags.id', $excluded);
    })->orWhereDoesntHave('tags');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question