Clone an existing Eloquent model instance into a new, unsaved model while resetting primary keys and timestamps.
When duplicating complex records (such as cloning an invoice, template, or draft post), copying individual attributes manually is error-prone.
The replicate() method creates a copy of an existing model instance, clearing primary keys and timestamp values automatically so it can be saved as a new database row.
Basic Cloning
use App\Models\Invoice;
$invoice = Invoice::find(1);
// Clones attributes into a new unsaved instance
$newInvoice = $invoice->replicate();
$newInvoice->invoice_number = 'INV-2026-002';
$newInvoice->save();
Excluding Specific Columns
Pass an array of attribute names to exclude them from the cloned model:
use App\Models\Post;
$post = Post::find(10);
// Duplicate the post, resetting view counts and publication status
$draft = $post->replicate([
'slug',
'views_count',
'is_published',
'published_at',
]);
$draft->title = 'Copy of ' . $post->title;
$draft->save();
Summary
- Clears the model primary key (
idor custom key) andcreated_at/updated_attimestamps. - Accepts an array of attribute exceptions to prevent sensitive or unique columns from copying.
- Returns a fresh, unsaved model instance ready for customization before calling
save().
Tags:
Laravel Eloquent Database Models