Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

php - 使用laravel雄辩地更新一对多(update one to many using laravel eloquent)

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

(我有两个模型,一个是Purchase.php,另一个是PurchaseItem.php。)

Purchase.php

(Purchase.php)

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

And PurchaseItem.php

(和PurchaseItem.php)

public function purchase()
{
    return $this->belongsTo('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

(谢谢)

  ask by Mahfuz Shishir translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

您必须创建一个采购表这样的接口表,并使用BlongstoMany函数来解决您的问题


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...