php—JSONContains Laravel5.6在哪里不工作?

fbcarpbf  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(333)
$rosters = EventRosters::where('event_id', $event_id)
    ->whereJsonContains('players', $user_id)
    ->whereNull('deleted_at')
    ->get();

上面雄辩的查询似乎只有在“players”json数组中只有一个条目时才起作用。
数据库中存储的数据如下所示: [1]["1","2"] jsonContains只有在看到时才起作用,这有什么原因吗 [1] 在数据库中,但当它看到 ["1","2"] ?
我对拉雷维尔还很陌生,一直在为这件事挣扎。

a7qyws3x

a7qyws3x1#

文件有点直截了当
https://laravel.com/docs/5.6/queries#json-where子句

$rosters = EventRosters::where('event_id', $event_id)
    ->whereJsonContains(['players', [1,2]])
    //->whereNull('deleted_at') Unless you setup a scope at the model's bootup, 
    //Eloquent won't fetch soft deleted records
    ->get();

根据json列中的内容(如果是id),替换 playersplayers->id

cgvd09ve

cgvd09ve2#

数据类型必须匹配:

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

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

相关问题