Laravel Eloquent ORM 复制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36021239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent ORM replicate
提问by Zoli
I have a problem with replicating one of my models with all the relationships.
我在使用所有关系复制我的模型之一时遇到问题。
The database structure is as follows:
数据库结构如下:
Table1: products
id
name
Table2: product_options
id
product_id
option
Table3: categories
id
name
Pivot table: product_categories
product_id
category_id
Relationships are:
关系是:
- product hasMany product_options
- product belongsToMany category (trough product_categories)
- 产品有许多 product_options
- 产品属于多类别(通过 product_categories)
I would like to clone a product with all the relationships. Currently here is my code:
我想克隆一个具有所有关系的产品。目前这是我的代码:
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->push();
foreach($product->options as $option){
$new_option = $option->replicate();
$new_option->product_id = $new_product->id;
$new_option->push();
}
But this does not works (the relationships are not cloned - currently I just tried to clone the product_options).
但这不起作用(关系没有被克隆——目前我只是试图克隆 product_options)。
回答by haakym
This code, worked for me:
这段代码对我有用:
$model = User::find($id);
$model->load('invoices');
$newModel = $model->replicate();
$newModel->push();
foreach($model->getRelations() as $relation => $items){
foreach($items as $item){
unset($item->id);
$newModel->{$relation}()->create($item->toArray());
}
}
Answer from here: Clone an Eloquent object including all relationships?
从这里回答:克隆一个包含所有关系的 Eloquent 对象?
This answer (same question), also works fine too.
这个答案(同样的问题)也很好用。
//copy attributes from original model
$newRecord = $original->replicate();
// Reset any fields needed to connect to another parent, etc
$newRecord->some_id = $otherParent->id;
//save model before you recreate relations (so it has an id)
$newRecord->push();
//reset relations on EXISTING MODEL (this way you can control which ones will be loaded
$original->relations = [];
//load relations on EXISTING MODEL
$original->load('somerelationship', 'anotherrelationship');
//re-sync the child relationships
$relations = $original->getRelations();
foreach ($relations as $relation) {
foreach ($relation as $relationRecord) {
$newRelationship = $relationRecord->replicate();
$newRelationship->some_parent_id = $newRecord->id;
$newRelationship->push();
}
}
From here: Clone an Eloquent object including all relationships?
从这里开始:克隆一个包含所有关系的 Eloquent 对象?
The code works fine for many to many relationships in my experience.
根据我的经验,该代码适用于多对多关系。
回答by alexw
回答by Luis Vasquez
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->{attribute} = {value};
$new_product->push();
$new_product->options()->saveMany($product->options);
回答by avinash
This worked fine on 5.5. image , media is a relation name.
这在 5.5 上运行良好。image , media 是一个关系名称。
$event = Events::with('image','media')->find($event_id);
if($event){
$newevent = $event->replicate();
$newevent->push();
foreach ($newevent->getRelations() as $relation => $entries)
{
foreach($entries as $entry)
{
$e = $entry->replicate();
if ($e->push())
{
$newevent->{$relation}()->save($e);
}
}
}
}