gpt4 book ai didi

php - 在 Laravel 中使用 JSON 列

转载 作者:可可西里 更新时间:2023-11-01 08:38:59 25 4
gpt4 key购买 nike

我有一个项目。目前,我正在使用 CV(简历)表。因此,我认为每份简历都可以包含一个或多个电话号码和电子邮件。

因为我已经有大约 7 个与 CV(如技能、语言等)相关的表格,所以我决定将电子邮件和电话保存在 Сv 中s 列,作为 JSON 列。

所以,在我的迁移中,我把

$table->json('phone');
$table->json('email');

我怎么能操纵这个

<input type="text" name="phone[]"> 
<input type="text" name="email[]">

用于将这些来自 Controller 的数据存储在数据库中,我应该如何在 View 中检索它们?

最佳答案

使用属性转换。在这种情况下,您不需要将数据从数组转换为 JSON 并返回。

来自 the docs :

The array cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a JSON or TEXT field type that contains serialized JSON, adding the array cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model

protected $casts = [
'phone' => 'array',
'email' => 'array',
];

关于php - 在 Laravel 中使用 JSON 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com