Laravel Eloquent JSON Contains, how to provide WhereIn logic (one of array values)

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Laravel Eloquent JSON Contains and WhereIn Logic: Exploring Possible Solutions for Filtering Array Values in JSON Arrays Body:

The Laravel framework offers several built-in tools to work with databases, including its Query Builder. One of these tools is the whereJsonContains() method, which allows us to perform "contains" logic on JSON arrays stored in MySQL database columns. This method is useful when we need to filter out records based on specific array elements contained within a JSON object. However, sometimes we might need to apply "of or" type of logic instead of the existing "of and" approach.

Basic Code Example

In your case, you have an array $room_count = [2,4], which represents a list of possible room counts for an advertisement. In MySQL database, the column rent_requests.rooms_count stores JSON arrays with room counts like [2,3]. The question is how to filter the records showing ads where any of the specified room counts in the array (in this case, either 2 or 4) is present within the MySQL JSON array.

builder ->whereJsonContains('rent_requests.rooms_count', $room_count); return $query; ?>

Although it seems like the code meets your needs, it actually filters out records where both 2 and 4 are present in rent_requests.rooms_count. The issue here is that `whereJsonContains()` expects to find all array elements (in this case, 2 and 4) within the JSON array, not just one of them.

Understanding Array Intersection

To solve this problem, you can use array intersection. The Laravel Collection class provides an intersect() method to find common elements in multiple arrays. You can use it like this:

builder ->where(function ($query) { $rent_requests_rooms_count = json_decode($this->rent_requests->rooms_count, true); $intersected = \Illuminate\Support\Collection::make($room_count)->intersect($rent_requests_rooms_count); if ($intersected->isEmpty()) { return false; } $query->whereIn('rent_requests.rooms_count', $intersected->toArray()); }); return $query; ?>

In the above code, we first decode the JSON array into a PHP array using json_decode(). Then, we use the intersect() method to find the common elements between our provided room counts and those stored in the database. If there are no common elements (i.e., the intersection is empty), we return false, which will filter out records where none of the specified room counts are present. Finally, if there is an intersection, we use whereIn() to filter our results based on these values.

Conclusion

Laravel's Query Builder offers various options for working with database queries, including built-in methods like whereJsonContains(). However, if you need to apply "of or" logic instead of "of and", you can combine the basic code example with array intersection using techniques shown above. This approach will help you filter records based on one or more of the specified room counts while working with Laravel's Query Builder effectively.