Laravel Eloquent ORM replicate

2020-02-13 11:41发布

问题:

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

回答1:

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.



回答2:

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


回答3:

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

$new_product->options()->saveMany($product->options);


回答4:

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

                }                                

            }