我有一个 JSON 字段,我正在尝试它,但我在使用它时遇到了一些问题。
我已将以下内容添加到我的 Customer
模型中:
protected $casts = [
'billingConfig' => 'array'
];
我在 Controller 中使用以下内容更新了测试字段:
$customer->billingConfig = ['attachableType' => $request->attachmentsConfig];
$customer->save();
此后,我的数据库中将出现以下内容:
{"attachableType": "combined"}
现在,当我通过 Blade 获取这个特定值时:
{{$customer->billingConfig->attachableType}}
我得到“尝试获取非对象的属性‘attachableType’”
但是当我使用以下内容时:
{{$customer->billingConfig['attachableType']}}
我得到了我正在寻找的“组合”值。
我正在使用本指南:https://www.qcode.in/use-mysql-json-field-in-laravel/ ,我想我想确保我做的每件事都是正确的,他们的方法是错误的,或者我在某个地方犯了错误。
最佳答案
JSON 数据字段被作为数组读取,您无法为其调用属性,这就是我认为调用时出现错误的原因
{{$customer->billingConfig->attachableType}}
因为它是一个数组,而不是一个数据对象(就像 JavaScript 中的情况)。
感谢分享。
关于mysql - 在 Laravel 中从 MySQL 字段插入和检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54581799/