亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Laravel 5.8 從動態表單更新數據

使用 Laravel 5.8 從動態表單更新數據

PHP
ibeautiful 2022-06-17 16:00:27
我有一些問題,但我不明白為什么它不起作用。我有一個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'    ];}
查看完整描述

1 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

首先,您必須定義表之間的關系。


//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');

};


查看完整回答
反對 回復 2022-06-17
  • 1 回答
  • 0 關注
  • 112 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號