laravel:json字段与数组的比较

8wtpewkr  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(522)
laravel-5.7/mysql

在我的数据库中,有一个json格式的字段,如下所示:
字段名称:功能:

[
    {"id": 1, "url": null, "name": "A"}, 
    {"id": 2, "url": null, "name": "B"}
]

同样在它的模型中,我写了这个

protected $casts = [
    'features' => 'array'
  ];

现在我创建一个数组:

$features = array();

temp = array();
temp['id'] = 1;
temp['url'] = null;
temp['name'] = A;
$features[] = temp;

temp = array();
temp['id'] = 2;
temp['url'] = null;
temp['name'] = B;
$features[] = temp;

我怎么比较 $features arrayfeatures field 在数据库里?
ّ我查了一下:

$fff = \App\Cart::whereFeatures($features)->get()->first();

$fff = \App\Cart::whereFeatures(json_encode($features))->get()->first();

或者

$fff = \App\Cart::whereFeatures(json_encode($features,JSON_UNESCAPED_UNICODE))->get()->first();
mpgws1up

mpgws1up1#

使用原始表达式强制转换比较值:

$fff = \App\Cart::whereRaw('features = cast(? as json)', json_encode($features))->get();

相关问题