我正在使用Laravel,我的问题是:如何在将数据插入另一个表时更新一个表中的值?我是否必须在控制器中编写新功能或更新现有的或者可能在表单级别上以eloquent(view?)进行更新?
例如:
这是我的OrderController.php函数处理插入:
public function insert(Request $request){
$order = new order;
$order->material_id = $request->material_id;
$order->invoice_id = $request->invoice_id;
$order->count_order = $request->count_order;
$order->save();
return redirect('/order');
}
order.blade.php表单插入数据如下:
<form method="POST" action="/order">
<div class="form-group">
<input list="invoices" name="invoice_id" autocomplete="off">
<datalist id="invoices">
@foreach($invoices as $invoice)
<option value="{{$invoice->id}}">{{$invoice->number}}</option>
@endforeach
</datalist>
<input list="materials" name="material_id" autocomplete="off">
<datalist id="materials">
@foreach($materials as $material)
<option value="{{$material->id}}">{{$material->name}}</option>
@endforeach
</datalist>
<input type="count" name="count_order" placeholder="Ilość zamówiona" autocomplete="off">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Dodaj zamówienie</button>
</div>
{{ csrf_field() }}
</form>
订单表有:
| id | count_order | material_id | invoice_id |
材料表有:
| id | name | count_all | count_current | price_unit |
现在,当我在插入 count_order 列时在表单中发布值时,我想更新材料中的列(添加值) count_all 订单表。
答案 0 :(得分:1)
你可以使用if
$query = $order->save();
if($query){
//do update data
}
答案 1 :(得分:0)
You can extend the save()
function in Order model, there you would have the value you need and you can perform an update in Materials