简体   繁体   English

Laravel “json_extract”数据没有来

[英]Laravel "json_extract" data is not coming

Here is the data saved in the extra_data column of a record from my FormElement model:这是保存在我的 FormElement model 记录的 extra_data 列中的数据:

"{\"title\":\"\\u0130nput Ba\\u015fl\\u0131\\u011f\\u0131 Say\\u0131\",\"slug\":\"input_basligi_sayi\",\"type\":\"1\",\"required\":null,\"rows\":1,\"data_type\":\"number\",\"options\":null,\"length\":\"col-xl-6\"}"

When I try to pull with the code below, it returns null.当我尝试使用下面的代码进行拉取时,它返回 null。

$formElements = FormElement::where(DB::raw("json_extract(extra_data, '$.type')"), 1)->get();

I tried this in several ways;我尝试了几种方法;

$formElements = FormElement::whereIn('id', $extraData)->whereJsonContains('extra_data', ['type' => 1])->get();
$formElements = FormElement::whereIn('id', $extraData)->whereJsonContains('extra_data->type', 1)->get();

How do I do this?我该怎么做呢? Thanx..谢谢..

Not sure in what case this issue不确定在什么情况下会出现此问题

Add this to the model and see将此添加到 model 并查看

protected $casts = [
    'extra_data' => 'json',
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM