Laravel数组到多个记录的字符串转换

时间:2019-04-13 15:08:45

标签: mysql laravel vue.js laravel-5 eloquent

我正试图一次保存多个记录。

当我尝试存储多条记录时,它给出了数组到字符串转换的错误。

这是我从axios得到的响应。

array:2 [
  0 => array:3 [
    "property_type_id" => 2
    "fee_value" => "123"
    "product_service_id" => 1
  ]
  1 => array:3 [
    "property_type_id" => 1
    "fee_value" => "3333"
    "product_service_id" => 1
  ]
]
$feeByPropertyType = $request->feeByPropertyType;

        foreach ($feeByPropertyType as $row)
        {
            $data[] = [
                'property_type_id' => $row['property_type_id'],
                'fee_value' => $row['fee_value'],
                'product_service_id' => 1
            ];
        }
PaymentTypeProductService::insert($data);

我也尝试了其他几种方法,但到目前为止都没有奏效。

我们将非常感谢社区提供的有关如何更正此问题的帮助。

2 个答案:

答案 0 :(得分:0)

您需要格式化$data才能如此。

[
  [
    'property_type_id' => 2,
    'fee_amount' => "123"
  ],
  [
    'property_type_id' => 1,
    'fee_amount' => "1312"
  ],
]

答案 1 :(得分:0)

在这种情况下,如果$request->feeByPropertyType数据结构的数据结构与数据库表结构匹配,那么您可以简单地使用DB门面一次插入多行。

DB::insert($request->feeByPropertyType);
相关问题