因此,我正在尝试将数据作为 JSON 存储在数据库中,我已经看到内置通知表正在执行此操作。
我创建了一个“文本”类型的字段并存储了这个(示例)
{"filenames":["D0BmpFOdyUp4YyA8tqpL60VxO9kXsfciP9eLhIxd.jpeg","nAAs1xeErQSxPGxPvP68bCMn4a0E3y4EcgwfCwJl.png"],"title":"asdasdasa","content":"adasdasa"}
它按预期返回字符串,我如何让它理解为数组?
我必须在模型中添加一些东西还是......?
谢谢。
最佳答案
参见 attribute Casting在 Laravel 的文档中。在您的 Eloquent 模型类中,添加一个 $casts
属性,例如:
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'your_column_name' => 'array'
];
来自 Laravel 文档:
The
$casts
property on your model provides a convenient method of converting attributes to common data types. The$casts
property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to.
关于php - 如何在 mysql laravel 中存储 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46740283/