php - laravel:JSON 字段与数组的比较
问题描述
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 array
比较features 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();
解决方案
使用原始表达式转换比较值:
$fff = \App\Cart::whereRaw('features = cast(? as json)', json_encode($features))->get();
推荐阅读
- android - ConstraintLayout 无法在 Kotlin 中强制转换为 TextView
- r - 绘制具有两个连续变量但 R 生成“提供给连续尺度的离散值”错误的散点图
- pandas - 循环创建 df 列,其值取决于列标题中的数字
- wordpress - 以代码格式收到强大的联系,图像和样式被剥离
- wordpress - 如何在列表视图中显示 woocommerc 产品?
- python - Python列表通配符与另一个列表匹配
- javascript - 根据滚动位置减小粘性标题的徽标大小
- python - 两列分组的熊猫百分比
- java - 在 Struts 2 中使用 JSP 自定义调试拦截器
- ios - CocoaPods 找不到 pod 的兼容版本