Yes I had the same issue with laravel 8. It returns empty array even though it's documented to be used like this:
$users = DB::table('users')
->whereJsonContains('options->languages', ['en', 'de'])
->get();
You can solve this issue like this:
Food::where(function ($query) use ($asian) {
foreach ($asian as $country) {
$query->orWhereJsonContains('countries', $country);
}
})->get();
and don't forget to add countries field into $cast variable in your model. This will instruct Eloquent to deserialize it from JSON into a PHP array:
class Food extends Model
{
...
protected $casts = [
'countries' => 'array'
];
...
}
Update
Although the above code solves the issue, I found that the whereJsonContains method works fine. I was inserting the data in a wrong way and I didn't get any error on that! Make sure you are inserting the data in a proper way as you wouldn't get an error if you try to insert to JSON data like this:
Food::factory()->create([
'countries' => 'USA'
]);
I tested this on Laravel 8, IDK if this happens on older versions.
Your data should be like this:
{
{
...
'countries': ['USA', 'China']
...
}
}
In this case this:
Food::whereJsonContains('countries', $asian)->get();
It would return Foods that their countries contained within $array.