使用 Laravel 5.8 从动态表单更新数据

我有一些问题,但我不明白为什么它不起作用。


我有一个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'

    ];

}


ibeautiful
浏览 91回答 1
1回答

眼眸繁星

首先,您必须定义表之间的关系。//QUOTATION Modelpublic function items(){&nbsp; &nbsp; return $this->hasMany('App\Item');}public function terms(){&nbsp; &nbsp; return $this->hasMany('App\TermCondition');}//ITEM Modelpublic function quotation(){&nbsp;&nbsp;&nbsp; &nbsp;return $this- belongsTo('App\Quotation','id_quotation');&nbsp;}//TermCondition Modelpublic function quotation(){&nbsp;&nbsp;&nbsp; &nbsp;return $this- belongsTo('App\Quotation','id_quotation');&nbsp;}现在在您的控制器中,当您成功更新报价时,然后更新其他表的其余部分。public function update(Request $request) {$quotation = Quotation::findOrFail($request->id);$quotation->no_quotation&nbsp; &nbsp; = $request->no_quotation;...if($quotation->update()) {&nbsp; $getQuotationID = $quotation->id;&nbsp; for ($i = 1; $i <= count($request->items); $i++) {&nbsp; &nbsp; $item = Item::where('id_quotation', $getQuotationID)->first();&nbsp; &nbsp; $item->id_quotation = $getQuotationID;&nbsp; &nbsp; ...&nbsp; &nbsp; $item->totalrate&nbsp; &nbsp; = $request->items[$i]['totalrate'];&nbsp; &nbsp; $item->save();&nbsp; };&nbsp; for ($i = 1; $i <= count($request->terms); $i++) {&nbsp; &nbsp; $term = TermCondition::where('id_quotation', $getQuotationID)->first();&nbsp; &nbsp; $term->id_quotation = $getQuotationID;&nbsp; &nbsp; $term->caption&nbsp; &nbsp; &nbsp; = $request->terms[$i]['caption'];&nbsp; &nbsp; $term->save();&nbsp; };return redirect('quotation-show');};
打开App,查看更多内容
随时随地看视频慕课网APP