samedi 30 novembre 2019

update one to many using laravel eloquent

I have two models one Purchase.php and PurchaseItem.php

Purchase.php

public function purchase_items()
{
    return $this->hasMany('PurchaseItem', 'purchase_id');
}

And PurchaseItem.php

public function purchase()
{
    return $this->belongsTo('App\Models\Purchase', 'purchase_id');
}

Users can purchase multiple items at a time that he wants. During update purchase, users can choose different items. In that case, some old items may remove and some new items added. So what is the best way to update the tables? I can remove all existing items and insert the newly added item during the update purchase. But I want to know the best practice in this case.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire