Laravel Packagev13.xMIT License0+ downloads

Deep Replicate: Copy the whole graph not just the model

Deep Replicate turns Eloquent's one-model replicate() into a full graph copy. You choose which relations are copied, reused or skipped, and the package inserts the new records, rewrites their keys, recreates the pivot rows, and commits it all in one transaction.

$composer require pharaonic/laravel-deep-replicate
Key Features

Everything you need to duplicate real data

Select the paths, call `replicate()`, and get the new root, an identity map and stats back.

Explicit copy, reuse and skip

Every relation path gets a strategy. Nothing outside your selection is ever traversed.

All direct relations

HasOne, HasMany, BelongsTo, BelongsToMany and every polymorphic relation, nested to any depth.

Faithful pivots

Every pivot row and column is recreated, custom using() pivot models and morph types included.

Identity map and cycles

A record reached twice is copied once, and a cycle back to the root resolves to the new root.

Atomic and protected

One transaction, protected linking keys, no re-parenting, and a full rollback on any failure.

Read-only dry run

dryRun() reports paths, projected counts and warnings without writing or firing events.

A quick look

Copy an order with its items and options, keep the products and the customer.

app/Http/Controllers/OrderController.php
1use Pharaonic\Laravel\DeepReplicate\Facades\DeepReplicate;
2 
3$result = DeepReplicate::for($order)
4 ->copy('items', 'items.options')
5 ->reuse('items.product', 'customer')
6 ->skip('payments')
7 ->with(['status' => 'draft'])
8 ->replicate();
9 
10$copy = $result->model(); // the new, persisted order
11$newItem = $result->map()->get($oldItem); // its copy
12$result->stats()->copiedModels(); // root + items + options
FAQ

Deep Replicate Questions

Quick answers about installing and using Deep Replicate.

What is Deep Replicate?

Replicate a chosen Eloquent relationship graph into new persisted records, with explicit copy, reuse and skip rules per relation path, in one transaction. It's a free, open-source Laravel package by Pharaonic.

How do I install Deep Replicate?

Run composer require pharaonic/laravel-deep-replicate in your project's root directory.

What does Deep Replicate require?

The latest release requires PHP 8.3+ and Laravel 13.

How is it different from Eloquent's replicate()?

replicate() returns one unsaved copy of one model. Deep Replicate copies the relations you select, links the copies to each other, recreates pivot rows and saves everything in one transaction.

Can I copy a model without any relation?

No. A deep replication always needs at least one relation selected as copy or reuse. Use Laravel's replicate() for the model alone.

What happens to unique columns like slugs?

They are never changed automatically. Give them a value with generate(), with() or transform(); otherwise the database rejects the duplicate and nothing is kept.

Is Deep Replicate free to use?

Yes. Deep Replicate is open source under the MIT license, so you can use it in personal and commercial projects.

Where can I find the Deep Replicate documentation?

Read the Deep Replicate documentation for setup, configuration, and usage examples.

How do I report a bug or contribute to Deep Replicate?

Open an issue or a pull request on GitHub, ask in GitHub Discussions, or chat with us on Discord.

Ready to dig in?

Read the full documentation for relation strategies, nested graphs, pivots, attribute rules, loaded relations, lifecycle callbacks, transactions, dry runs, limits and troubleshooting.