[英]Storing arrays into database as Single Value - Laravel
I am trying to send this array of values below to my database and i get the error 我试图将下面的数组值发送到我的数据库,我得到错误
Argument 1 passed to Illuminate\\Database\\Grammar::parameterize() must be of the type array, string given 传递给Illuminate \\ Database \\ Grammar :: parameterize()的参数1必须是类型数组,字符串给定
Array of values 值数组
Controller 调节器
public function store(Request $request, $user_id, $broker_id)
{
$get_item_id = $request->all();
$store_item = $this->patient->store(array_merge($get_item_id, ['user_id' => $user_id], ['broker_id' => $broker_id ]));
return response()->json(['success' => true, 'data'=> ['items' => $get_item_id)]],200);
}
Model 模型
protected $fillable = ['item_id'];
When i post the array as shown in the image to i get the error above..What could i be doing wrong in my code ? 当我发布图像所示的数组时,我得到上面的错误。我的代码中我可能做错了什么?
PS: I am trying to store item[0]
and item[1]
into the database. PS:我试图将item[0]
和item[1]
到数据库中。
New Code 新规范
public function store(Request $request, $user_id, $broker_id)
{
$get_item_id = $request->all();
foreach($get_item_id as $data)
{
$store_item = $this->patient->store(array_merge($data, ['user_id' => $user_id], ['broker_id' => $broker_id ]));
}
return response()->json(['success' => true, 'data'=> ['items' => $get_item_id)]],200);
}
Results 结果
The item_id column is empty and it only return one row.. item_id列为空,只返回一行..
Try updating your for-loop to reflect the array you want to loop over 尝试更新for循环以反映要循环的数组
$get_item_id = $request->all(); // returns {"item_id": ["1", "2"]}
foreach($get_item_id['item_id'] as $data) // data will first be "1" then "2"
{
// store the data
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.