Laravel Eloquent ORM复制

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)

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).

dongxidui1227
dongxidui1227 你能把评论转移到答案吗?
4 年多之前 回复
drpiqlzrh62917192
drpiqlzrh62917192 toolong
4 年多之前 回复

4个回答

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?

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?

The code works fine for many to many relationships in my experience.

Try using attach to create the relationship:

foreach($product->options as $option){
    $new_option = $option->replicate();
    $new_option->save();
    $new_option_id = $new_option->id;
    $new_product->options()->attach($new_option_id);
}

This worked fine on 5.5. image , media is a relation name.

$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);
                        }
                    }

                }                                

            }
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->{attribute} = {value};
$new_product->push();

$new_product->options()->saveMany($product->options);
dongyan7988
dongyan7988 您应该解释您的答案,以便提问者(以及找到它的任何其他人)了解此代码的作用。
4 年多之前 回复
立即提问
相关内容推荐