Laravel-来自api的数组发布以"Array"的形式保存到数据库中如何获得数组项 [英] Laravel - array post from api saves to database as "Array" how to get array items
本文介绍了Laravel-来自api的数组发布以"Array"的形式保存到数据库中如何获得数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个外部api,它将POST发送到我的服务器,POST正文以数组的形式出现在json响应中
I have an external api that send a POST to my server the POST body comes in a json response as an array
{
"IDs": [
1378143,1378141,1971403,1451025,1451026,1374561,1378036,1371423,1371424,
1971568,1371426,1718539,1718515,1831163,1718540,1362068,1376341,1378221,
1378222,1378220,1380468,1380467,1417322,1385612,1971567
]
}
我将帖子保存如下:
$Id = new IdModel();
$Id->id = $request['IDs'];
$Id->save();
这会将数组在我的数据库列ID中保存为"Array",我如何才能保存数组中的所有值而不是单词Array?
This save the array in my database column id as "Array" how can I get all the values in the array to save instead of the word Array?
推荐答案
使用属性转换.打开您的 IdModel 模型并添加以下内容:
Use attribute casting. Open your IdModel model and add this:
protected $casts = [
'id' => 'array'
];
每次从数据库中获取数组并将其保存到数据库中时,都会转换数组.
This will convert the array every time you get it from the DB and every time you save it to the DB.
这篇关于Laravel-来自api的数组发布以"Array"的形式保存到数据库中如何获得数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文