Skip to content
Advertisement

whereJsonContains Laravel 5.6 not working?

$rosters = EventRosters::where('event_id', $event_id)
    ->whereJsonContains('players', $user_id)
    ->whereNull('deleted_at')
    ->get();

The eloquent query above seems to only work when there is a single item in the ‘players’ json array.

The data stored in the database looks as follows: [1] vs ["1","2"]

Is there a reason the whereJsonContains is only working when it sees [1] in the db but not when it sees ["1","2"] ?

I am pretty new to Laravel and have been struggling with this one a bit.

Advertisement

Answer

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.
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement