我有一些JSON数据;我将JSON数据解码为数组,现在我需要将链接列,id列和用户名列保存到我的数据库表中(表名可能是产品)。那么如何在Laravel中将此列插入/保存到数据库中?我的数组结构格式如下。
{#154 ▼
+"pagination": {#152}
+"meta": {#182 ▶}
+"data": array:20 [▼
0 => {#183 ▼
+"attribution": null
+"tags": array:11 [▶]
+"type": "image"
+"location": null
+"filter": "Normal"
+"created_time": ""
+"link": "https://www.url.com/p/myimage1/"
+"id": "4354"
+"user": {#192 ▼
+"username": "user"
+"profile_picture": "https://mypic.jpg"
+"id": "224"
+"full_name": ""
}
}
1 => {#193 ▼
+"attribution": null
+"tags": array:9 [▶]
+"type": "image"
+"location": null
+"filter": "Normal"
+"created_time": ""
+"link": "https://www.url.com/p/myimage2/"
+"id": "1234"
+"user": {#202 ▼
+"username": "user"
+"profile_picture": "https://mypic.jpg"
+"id": "224"
+"full_name": ""
}
}

答案 0 :(得分:2)
一种方法是保存原始json数据或使用->toJson()
或json_encode()
将数组转换为json,并将其作为Json数据保存到DB中:
$table->json('options'); // JSON equivalent for the database.
答案 1 :(得分:1)
您可以使用toJson()
方法,如下所示:
$table->toJson('options');