How to Copy or Duplicate Table Row Using Eloquent

Copy Everything From the Existing Row:


Copy Existing Product With New Name:

When the name of the table must be unique above method will thrown an error. For this use following approach.

//Query (fetch) a existing product
$existingProduct = Product::find($productId);

//Clone a attribute of existing row into a new copy
$duplicateProduct = $existingProduct->replicate();

//Change a name of the copy (duplicate) product by appending id of the existing product to make the name of duplicate product unique
$duplicateProduct = $existingProduct->name . " " . $existingProuct->id;

//Now Create a duplicate product