Search code examples
databaseobjectlaraveleloquentreplicate

Retrieving the object from a replicated row with Laravel/Eloquent


I'm replicating a database row and want to return the new row as an object.

$new_foo = Foo::find($id)->replicate()->save();
print_r($new_foo);

This returns 1 instead of the new object I just created. Thoughts?


Solution

  • In your code, you are actually saving the value returned from the save() method and not the replicated model.

    You need to make a slight change to your code to save the replicated model into $new_foo

    $new_foo = Foo::find($id)->replicate();
    $new_foo->save();
    dd($new_foo);
    

    save() method returns boolean.