3 years ago

whereHas where multiple (must contain)

Posted 3 years ago by ilaurens

I'm in a bit of a situation with whereHas, the problem is I got a array with keys, I need to use those selected key to find results that contains all (ids that is contained within the array). The problem is it works with one, but when having more than one it does not work.

It seems when using where('id', 3) it will select that one, but when you use another where('id', 5) it does not work (it probably uses the same record for that)

This is the code:

        $keys = array_keys($request->genres);

        $list = Movie::with('genres')->whereHas('genres', function ($query) use($keys) {
                $query->where('id', 3)->where('id', 5); // testing it manually, but it must be done with the value in "$keys"

It also has a pivot table, for relationship based on movie_id. If there is any other way, I'm hoping to hear that too. My preference is the best, clean and without much overhead.

Thank you in advance :)

Please sign in or create an account to participate in this conversation.