Laravel将多个表单保存到数据库

时间:2016-12-04 06:42:22

标签: php mysql laravel

我想用laravel将多个输入表单保存到数据库,当我用dd检查它没关系,但是如何将它保存到数据库中?

这是我的控制器

 public function store(Request $request){
  $input = Input::all();
    $servicedata = $input['service_id'];
    $employeedata = $input['emp_id'];
    $allOrder = array();

    foreach ($employeedata as $key => $val) {
        $detailorder = new DetailOrder;
        $detailorder->employee_id = $input['emp_id'][$key];
        $detailorder->mandays = $input['mandays'][$key];

        foreach ($servicedata as $key => $val) {

            $detailorder->sales_order_id = $request->sales_order_id;
            $detailorder->serivce_id = $input['service_id'][$key];
            $detailorder->order_type = $input['order_type'][$key];
            $detailorder->select_plan = $input['select_plan'][$key];
            $detailorder->qty = $input['qty'][$key];
            $detailorder->unit_price = $input['unit_price'][$key];
            $detailorder->note = $input['note'][$key];
        }
        $allOrder[]=$detailorder;
    }
     //dd($allOrder);
    $allOrder->save();
}

当我尝试使用$detailorder->insert($allOrder);时,我收到的错误就是这样

  

SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'0'(SQL:插入detail_orders0)值({“employee_id”:“2” ,“mandays”:“3”,“sales_order_id”:“1”,“serivce_id”:“2”,“order_type”:“更改”,“select_plan”:“计划2”,“数量”:“1”, “unit_price”:“300”,“note”:“test 2”}))

- >编辑

我的dd结果

array:2 [?
  0 => DetailOrder {#262 ?
#attributes: array:9 [?
  "sales_order_id" => "4"
  "serivce_id" => "1"
  "order_type" => "add"
  "select_plan" => ""
  "qty" => "1"
  "unit_price" => "200"
  "note" => ""
  "employee_id" => "2"
  "mandays" => "3"
]

1 => DetailOrder {#268 ?
#attributes: array:9 [?
  "sales_order_id" => "4"
  "serivce_id" => "2"
  "order_type" => "change"
  "select_plan" => ""
  "qty" => "1"
  "unit_price" => "300"
  "note" => ""
  "employee_id" => "2"
  "mandays" => "3"
]

我的桌面捕捉 enter image description here

2 个答案:

答案 0 :(得分:2)

您可以在循环中保存

'" & Format(DateTime.Parse(dgvData.Item("POLOG_DATE", i).Value.ToString), "MMM/dd/yyyy") & "',

答案 1 :(得分:1)

在订单模型中:

Protected $fillable = ['employee_id', 'mandays', 'sales_order_id', 'service_id', 'order_type', 'select_plan', 'qty', 'unit_price', 'note'];

在您的控制器上,请尝试在第一个foreach之外使用$ allOrder的foreach循环:

public function store(Request $request)
{
    $input = Input::all();
    $servicedata = $input['service_id'];
    $employeedata = $input['emp_id'];
    $allOrder = array();

    foreach ($employeedata as $key => $val) {
        $detailorder = new DetailOrder;
        $detailorder->employee_id = $input['emp_id'][$key];
        $detailorder->mandays = $input['mandays'][$key];

        foreach ($servicedata as $key => $val) {

            $detailorder->sales_order_id = $request->sales_order_id;
            $detailorder->serivce_id = $input['service_id'][$key];
            $detailorder->order_type = $input['order_type'][$key];
            $detailorder->select_plan = $input['select_plan'][$key];
            $detailorder->qty = $input['qty'][$key];
            $detailorder->unit_price = $input['unit_price'][$key];
            $detailorder->note = $input['note'][$key];
        }
        $allOrder[] = $detailorder;
    }
    //dd($allOrder);

    foreach ($allOrder as $order) {
        YourOrderModelName::create($order);
    }

}