Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 504
Meta Avatar asked Dec 07 '22 14:12

Meta


1 Answers

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.
like image 130
Jonas Staudenmeir Avatar answered Dec 11 '22 11:12

Jonas Staudenmeir