Wherejsoncontains Laravel 5.6 Not Working

whereJsonContains Laravel 5.6 not working?

The data types have to match:

// [1, 2]
->whereJsonContains('players', 1) // Works.
->whereJsonContains('players', '1') // Doesn't work.

// ["1", "2"]
->whereJsonContains('players', '1') // Works.
->whereJsonContains('players', 1) // Doesn't work.

Laravel whereIn implementation of whereJsonContains

You need to follow your query as below.

$array = [0 => 1, 1 => 2, 2 => 3];

// Eloquent
PaymentTransaction::whereJsonContains('payload->ProductCode->id',$array)->get();;

// or
PaymentTransaction::jsonContains('payload->ProductCode->id', $array)->get();

you can try it as below too.

$array = [0 => 1, 1 => 2, 2 => 3];
$array = array_values(array_map('strval',$array));
PaymentTransaction::where(function ($query) use ($array) {
foreach ($array as $id) {
$query->orWhereJsonContains('payload->ProductCode->id', $id);
}
})->get();

JSON_CONTAINS doesn't recognize my column mysql v.8.0.17 laravel 5.6

The problem is empty string that you are passing as argument.

Try:

SomeModel::whereRaw("JSON_CONTAINS(column, null, '$.a')")->get();

Get records from a table where an array of deleted_organization_id does not contain organization id using eloquent laravel 5.6

This is what you are looking.

$posts = Post::whereRaw("not json_contains(deleted_for_organization , '[".Auth::user()->organization_id."]') OR deleted_by_org is NULL")->get();

How can I select data write where in json object in related ORM Laravel

When you use with in Laravel Eloquent, it doesn't grab the other table, it creates 2 queries, so you can't query like this.

You can use :

$basket = Basket::whereHas('product', function ($query) use ($stores_id) {
$query->whereJsonContains('store->id', $stores_id);
})->get();

Or use JOIN statement.



Related Topics



Leave a reply



Submit