lundi 10 août 2020

Laravel orWhereIn and orWhereIn and so on

To be honest I rly do not know how to describe my query problem, hope example will clear things up:

I've got simple query:

return $query->whereHas('categories', function ($q) use ($ids) {
    $q->whereIn('category_id', $ids);
}, '=', count($ids));

which returns categories that match array of ids. To present this better right now we've got such condition

getCategoriesByIdThatMatchThisCondition(id1 && id2 && id3... and so on).

What I want is to achieve such comparasion

getCategoriesByIdThatMatchThisCondition((id1.1 || id1.2 || id1.3) && (id2.1 || id2.2) ... and so on.)

I've couldn't find any help with this problem so far. Even naming such query would be helpfull. My table is simple PIVOT

id    record_id    category_id
------------------------------------
1         1            35
2         2            41
3         2            74
4         3            74
5         3            40
6         4            40

Summarizing my problem with words:

I do not have record relation to parent_category of selected category... Right now I need to display all records that match multiple categories.

Visualising problem:

Record is assigned to categories like this:

35
 └40
 └41 ☑
36
 └74 ☑

By providing id 35 and 36, record should be found! By providing 40, 74 should NOT be found...

Hope I've described my problem well enough.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire