json - Laravel:json 编码在数据库更新中返回空
问题描述
当我在 laravel 的更新查询生成器中使用 json_encode 时,它返回空。我试过的代码是这样的:
DB::table('catalog_product')->update([
'reference' => json_encode(['data' =>DB::raw('external_barcode')]),
]);
我该怎么做呢?
解决方案
正如我所说,您将 PHP 函数与 MYSQL 值混合在一起。
或者你使用 PHP 方式:
$products = CatalogProduct::get();
foreach ($products as $product) {
$product->reference = json_encode(['data' => $product->external_barcode]);
$product->save();
}
或使用 MYSQL :
DB::table('catalog_product')->update([
'reference' => DB::raw('JSON_OBJECT("data", external_barcode)'),
]);
推荐阅读
- angular - 单页应用 Angular 4
- javascript - 如何使用 jest 模拟 window.navigator.language
- javascript - JS 对象查询语法
- drupal - Drupal 8 翻译网络表单字段的标签
- java - 在 G1 GC 的不同区域(Eden、Surviver、old Gen.)中有多少以及如何划分
- c++ - 河内印刷塔(带台阶和塔)
- vba - 无法打开用 VBA 保存的 Word .docm 文档
- node.js - 在 Mongoose(Mongodb)中隐藏“_V”(versionKey)字段的结果是什么?
- docker - 如何从我的系统中清除我之前的 docker 项目?
- android - Android位图传递问题