• 7

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Laravel Eloquent ORM replicate

I have a problem with replicating one of my models with all the relationships.

The database structure is as follows:

Table1: products

Table2: product_options

Table3: categories

Pivot table: product_categories

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();
foreach($product->options as $option){
    $new_option = $option->replicate();
    $new_option->product_id = $new_product->id;

But this does not works (the relationships are not cloned - currently I just tried to clone the product_options).

This code, worked for me:

$model = User::find($id);


$newModel = $model->replicate();

foreach($model->getRelations() as $relation => $items){
    foreach($items as $item){

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

From here: Clone an Eloquent object including all relationships?

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

  • 11
Reply Report

Try using attach to create the relationship:

foreach($product->options as $option){
    $new_option = $option->replicate();
    $new_option_id = $new_option->id;
  • 2
Reply Report
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->{attribute} = {value};

  • 2
Reply Report
    • You should explain your answer so the asker (and anyone else who finds it) learns what this code does.
    • using saveMany that way doesn't add new records, that just overwrite the foreign key with the value of the new model

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

$event = Events::with('image','media')->find($event_id);
                $newevent = $event->replicate();
                 foreach ($newevent->getRelations() as $relation => $entries)
                    foreach($entries as $entry)
                        $e = $entry->replicate();
                        if ($e->push())


  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions