"Duplicate this" buttons look easy: copy the row, save it, done. Then the first copy fails with a duplicate-key error, or the new post comes out already deleted, or it quietly carries over an API token and a view count from the original. Eloquent has a built-in method for this, replicate() , and it handles more than people expect. It also leaves a few traps. I ran every example on Laravel 13.35.0 against a real database, so the results below are what actually happened.
The basic example
This one comes from the Laravel documentation. A shipping address becomes a billing address with one changed field:
$shipping = Address :: create ([ 'type' => 'shipping' , 'line_1' => '123 Example Street' , 'city' => 'Victorville' , ]);
$billing = $shipping -> replicate () -> fill ([ 'type' => 'billing' , ]);
$billing -> save ();
Enter fullscreen mode
Exit fullscreen mode
After this, the table had two rows: id 1 ( shipping ) and id 2 ( billing ), both with the same street.…