使用 Laravel 5.8 从动态表单更新数据
Posted
技术标签:
【中文标题】使用 Laravel 5.8 从动态表单更新数据【英文标题】:Update Data From Dynamic Form Using Laravel 5.8 【发布时间】:2020-04-08 16:17:17 【问题描述】:我有一些问题,但我不明白为什么它不起作用。
我有一个 Quotation.php 模型,例如:
class Quotation extends Model
protected $table = 'quotation';
protected $fillable = [
'no_quotation',
'subject',
'to',
'qutation_date',
'expire_date',
'pickup',
'destination',
'via',
'customer',
'address',
'totalcost',
'subtotal',
'tax',
'grandtotal'
];
Item.php 模型,例如:
class Item extends Model
protected $table = 'item';
protected $fillable = [
'id_quotation',
'name',
'qty',
'cost',
'totalcost',
'rate',
'totalrate'
];
TermCondition.php 模型,如:
class TermCondition extends Model
protected $table = 'term_and_condition';
protected $fillable = [
'id_quotation',
'caption'
];
和 QuotationController.php 类似:
public function update(Request $request)
$quotation = Quotation::findOrFail($request->id);
$quotation->no_quotation = $request->no_quotation;
$quotation->subject = $request->subject;
$quotation->quotation_date = $request->quotation_date;
$quotation->expire_date = $request->expire_date;
$quotation->pickup = $request->pickup;
$quotation->destination = $request->destination;
$quotation->via = $request->via;
$quotation->to = $request->to;
$quotation->customer = $request->customer;
$quotation->destination = $request->destination;
$quotation->address = $request->address;
$quotation->totalcost = $request->totalcost;
$quotation->subtotal = $request->subtotal;
$quotation->tax = $request->tax;
$quotation->grandtotal = $request->grandtotal;
$quotation->status = 'SUBMIT';
$quotation->update();
$deleteItems = Item::where('id_quotation', $request->id)->delete();
$deleteTerms = TermCondition::where('id_quotation', $request->id)->delete();
for ($i = 1; $i <= count($request->items); $i++)
$item = new Item;
$item->id_quotation = $request->id;
$item->name = $request->items[$i]['name'];
$item->qty = $request->items[$i]['qty'];
$item->cost = $request->items[$i]['cost'];
$item->totalcost = $request->items[$i]['totalcost'];
$item->rate = $request->items[$i]['rate'];
$item->totalrate = $request->items[$i]['totalrate'];
$item->save();
for ($i = 1; $i <= count($request->terms); $i++)
$term = new TermCondition;
$term->id_quotation = $request->id;
$term->caption = $request->terms[$i]['caption'];
$term->save();
return redirect('quotation-show');
在 routes.php 中像:
Route::post('/quotation-update/', 'QuotationController@update');
最后,我将 QuotationController.php 中的数据返回到 Json
,例如:
"_token": "emShldN8ecxrPzV9zhX64IlFk8ZPelPFwCLNd7zl",
"id": "1",
"no_quotation": "QUOT/12/12/2020",
"subject": "Installation Air Conditioner",
"quotation_date": "2019-12-12",
"expire_date": "2019-12-26",
"pickup": "Jakarta",
"destination": "Bandung",
"via": "Land - CDD",
"to": "Helmi Wantono",
"customer": "PT. EDI Indonesia",
"address": "Jl. Yos Sudarso Kav 18, Sunter Jaya. Sunter, Jakarta Utara.",
"items":
"1":
"name": "Air Conditioner",
"qty": "2",
"cost": "2,000",
"totalcost": "4,000",
"rate": "2,500",
"totalrate": "5,000"
,
"2":
"name": "Compressor",
"qty": "2",
"cost": "1,000",
"totalcost": "2,000",
"rate": "1,500",
"totalrate": "3,000"
,
"totalcost": "6,000",
"subtotal": "8,000",
"tax": "80",
"grandtotal": "7,920",
"terms":
"1":
"caption": "Price exclude insurance."
,
"2":
"caption": "Price exclude packing service."
此代码仅适用于更新报价信息,不适用于项目和条款。 这种情况下最好的方法是什么?谢谢
【问题讨论】:
尝试使用关系和 request() 助手让你的代码优雅... 【参考方案1】:首先你必须定义表之间的关系。
//QUOTATION Model
public function items()
return $this->hasMany('App\Item');
public function terms()
return $this->hasMany('App\TermCondition');
//ITEM Model
public function quotation()
return $this- belongsTo('App\Quotation','id_quotation');
//TermCondition Model
public function quotation()
return $this- belongsTo('App\Quotation','id_quotation');
当您成功更新报价时,现在在您的控制器中,然后更新其他表的其余部分。
public function update(Request $request)
$quotation = Quotation::findOrFail($request->id);
$quotation->no_quotation = $request->no_quotation;
...
if($quotation->update())
$getQuotationID = $quotation->id;
for ($i = 1; $i <= count($request->items); $i++)
$item = Item::where('id_quotation', $getQuotationID)->first();
$item->id_quotation = $getQuotationID;
...
$item->totalrate = $request->items[$i]['totalrate'];
$item->save();
;
for ($i = 1; $i <= count($request->terms); $i++)
$term = TermCondition::where('id_quotation', $getQuotationID)->first();
$term->id_quotation = $getQuotationID;
$term->caption = $request->terms[$i]['caption'];
$term->save();
;
return redirect('quotation-show');
;
【讨论】:
尝试更新你的循环。但我不确定您的数据是如何来自表单的。if ($it = $request->get('items')) foreach($it as $i) // Update logic goes here for item
。并为 Term Condition 做同样的事情。也尝试将你的 $i 设置为 0 而不是 1。
感谢您的回答,我在输入名称形式的数组输入中发现了错误的代码,然后返回错误偏移量。我在表单更新name="item[$i][index]"
处循环,但在动态表单中我写name="items[$i][index]"
我很高兴你发现了这个小错误。以上是关于使用 Laravel 5.8 从动态表单更新数据的主要内容,如果未能解决你的问题,请参考以下文章
laravel 5.8 在 getenv 上返回 false
如何修复 Laravel 5.8 Ajax 表单提交中的内部服务器错误
从 Laravel 5.8 升级到 6.2 后 ConfirmPasswordController 不存在