How to get rows having exact array values in one-to-many relationship MySQL/Laravel

Viewed 47

I have tried the query and got a list of all offer requests with sub material id='12,13'. But I want to get only rows that have exactly these sub materials(one to many relations). That means it should not return rows with sub material 12,13,14 or 12,13,14,15,16.

SELECT *
FROM offerrequests LEFT JOIN offerrequest_submaterials_mappings ON 
    offerrequests.id = 
    offerrequest_submaterials_mappings.offerrequest_id
WHERE offerrequest_submaterials_mappings.submaterial_id='12,13' 
GROUP BY offerrequests. id;

I have also tried laravel eloquent.

    $submaterials_array = [12,13];
    $otherrequests = for ($i = 0; $i < sizeof($submaterials_array); $i++) {
        $id = $submaterials_array[$i];
        Offerrequest::whereHas('submaterialprops', function ($q) use ($id) {
            $q->where('submaterial_id', $id);
        });
    }
    $otherrequests = $otherrequests->get();

The result is the same in both cases. I want exact relation values. Not a subset

1 Answers
Offerrequest::whereHas('submaterialprops', function ($q) use ($submaterials_array) {
    $q->whereIn('submaterial_id', $submaterials_array);
});
Related