php - 如何在 mysql laravel 中存储 JSON

标签 php mysql json laravel

因此,我正在尝试将数据作为 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/

相关文章:

php - 未知方法 SfGuardUserTable::retrieveByUsername Symfony1.4

php - 要创建一个安全的 PHP/MySQL 网站,最重要的事情是什么?

php - Laravel 5.1 SQLSTATE[23000] : Integrity constraint violation error

mysql - "Every derived table must have its own alias"Mysql错误

mysql - 将 6 个项目加在一起得出总数,但不要将它们的值加在一起

php - 在 s3 分段上传上设置内容类型元数据

java - hibernate 在选择期间锁定表多长时间?

Python 替换未知结构 JSON 文件中的值

c# - 如何在 C# 中将复杂的 geo-json 转换为 wkt?

python - 将 HTML 表转换为 JSON