Laravel路径资源无法更新值

时间:2016-10-07 13:26:55

标签: php laravel

是我在插入之前的数据库中的表单和获取值之一:

<form class="form-horizontal bordered-group" role="form" action="{{ route('invoice.update' , $invoice->id)}}" method="PUT" enctype="multipart/form-data">
        {{ csrf_field() }}
          <input name="_method" type="hidden" value="GET">

  <div class="form-group clear"></div>
  <div class="row">
  <div class="form-group form-inline">
    <label class="col-sm-4" >Subtotal: &nbsp;</label>
    <div class="input-group col-sm-6">
      <div class="input-group-addon">Tk.</div>
      <input name="subtotal" type="number" class="form-control" id="subTotal" value="{{ $invoice->subtotal}}" required>
    </div>
  </div>

  <div class="form-group form-inline">
    <label class="col-sm-4"><button type="button" id="total" class="btn btn-primary">Total</button></label>
    <div class="input-group col-sm-6">
      <div class="input-group-addon">Tk.</div>
      <input name="total" type="number" class="form-control" id="totalAmount" value="{{ $invoice->total}}" required>
    </div>
  </div>

    </div>
    </div>
    <div class="modal-footer no-border clear">
      <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      <button type="submit" class="btn btn-success">Update</button>
    </div>

</div>

   

是我的控制者之一:

public function update($id)
{
    $invoice = Invoice::find($id);
    $invoice->subtotal = $request['subtotal'];
    $invoice->total = $request['total'];
    $invoice->save();
    return redirect()->back()->with(['success' => 'Invoice Updtaed Successfully'] );
}

我如何解决我的问题并更新我的数据库值?

2 个答案:

答案 0 :(得分:3)

将代码更改为:

<form class="form-horizontal bordered-group" role="form" action="{{ route('invoice.update' , $invoice->id)}}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="Put">

您的表单方法始终为POST_method输入元素包含HTTP verbs(放置,删除等)。

此外,您应该注入Request对象以使其正常工作:

public function update(Request $request, $id)

答案 1 :(得分:1)

您指的是$ request,但您没有传递此变量,请尝试:

public function update(Request $request, $id)
{
    $invoice = Invoice::find($id);
    $invoice->subtotal = $request->get('subtotal');
    $invoice->total = $request->get('total');
    $invoice->save();
    return redirect()->back()->with(['success' => 'Invoice Updtaed Successfully'] );
}

或者,使用正确的验证和ACL创建自己的Request类,并将其实例传递给更新方法。