如何在laravel中更新具有相同名称的多个表单输入?

时间:2019-05-27 11:06:43

标签: php laravel

我在Laravel中通过POST更新数组时遇到问题。我已经捕获了数组中的所有发布数据,当更新一行重复更新整个循环时。

我试图在更新函数中回显我的$ request参数,它的返回数据包括所做的更改,但无法更新。

控制器:

public function update(Request $request, $id){
      $customr_details =  $request->customer;
      $customer_id = Customer::where('name', $customr_details)->first()->id;

      $new_id = Invoice::where('id', $id)->first()->id;

      $validator =  Validator::make($request->all(),[
        'invoice_no'         => 'required|unique:invoices,invoice_no,'.$new_id,
        "invoice_date"       => 'required',
        "billing_company"    => 'required',
        "due_date"           => 'required',
        "due_date"           => 'required',
        "carrier"            => 'required',
        "name"               => 'required',
        "date"               => 'required',
        "fare"               => 'required',
        "route"              => 'required',
        "carrier"            => 'required',
    ]);

    if($validator->fails()){
      return redirect()->back()->withErrors($validator);
    }

    $invoice = Invoice::find($id);
      $data1 = array(
        'customer_id'     => $customer_id,
        'date'            => $request->input('invoice_date'),
        'company'         => $request->input('billing_company'),
        'invoice_no'      => $request->input('invoice_no'),
        'customer'        => $request->customer,
        'due_date'        => $request->input('due_date'),
        'status'          => $request->input('status'),
        'total'           => $request->input('grand_total')
      );



      for ($i=0; $i < count($request->name); $i++) { 
        DB::table('services')
          ->where('invoice_id', $request->$id[$i])
          ->update([
            'invoice_id'  => $id,
            'pass_name'   => $request->name[$i],
            'date'        => $request->date[$i],
            'fare'        => $request->fare[$i],
            'route'       => $request->route[$i],
            'carrier'     => $request->carrier[$i],
            'remark'      => $request->remark[$i]
          ]);
      }


      $invoice->push($data1);
      return redirect()->back()->with('success', 'Update Successful!');
    }

查看:

<?php

$n = 0;
if (count($invoice_service) > 0) {
    foreach ($invoice_service as $inserv) {
        $n++;
?>
       <tr id="row_{{$n}}">
        <td>{{ $n }}</td>
        <td><input type="text" name="date[]" value="{{ $inserv->date }}" id="date" style="width: 100px;" class="form-control text-center input-sm">
        </td>
        <td>
        <div class="input-group">
        <input type="text" name="name[]" value="{{ $inserv->pass_name }}" id="name" class="form-control input-sm" maxlength="80" style="width: 200px;" autocomplete="off">
        </div>
        </td>
        <td><input type="text" name="route[]" value="{{ $inserv->route }}" id="route" style="width: 100px;" class="price form-control text-right input-sm">
        </td>
        <td><input type="text" name="carrier[]" value="{{ $inserv->carrier }}" id="carrier" style="width: 100px;" class="price form-control text-right input-sm">
        </td>
        <td><input type="text" name="fare[]" value="{{ $inserv->fare }}" id="fare" style="width: 200px;" class="price form-control text-right input-sm">
        </td>
        <td><input type="text" name="remark[]" value="{{ $inserv->remark }}" id="remark" style="width: 150px;" class="subtotal form-control text-right input-sm" ></td>
        </tr>

        <?php

    }
}

?>

没有错误,所有数组结果的第一行都会更新。

0 个答案:

没有答案