Search by

pharaonic / laravel-deep-replicate

Pharaonic

Replicate a chosen Eloquent relationship graph into new persisted records, with explicit copy, reuse, and skip rules per relation path.

Package info

github.com/Pharaonic/laravel-deep-replicate

Documentation

pkg:composer/pharaonic/laravel-deep-replicate

Fund package maintenance!

MoamenEltouny

Buy Me A Coffee

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0


README

PHP Version : 8.3 | 8.4 | 8.5 Laravel Version : 13.x License Build
Source Packagist Version Packagist Downloads

Replicate a chosen Eloquent relationship graph into new records, with explicit copy, reuse and skip rules.


Installation

composer require pharaonic/laravel-deep-replicate

The service provider and the DeepReplicate facade are registered automatically. There is nothing to publish or migrate.

Usage

use Pharaonic\Laravel\DeepReplicate\Facades\DeepReplicate;

$result = DeepReplicate::for($order)
    ->copy('items', 'items.options')   // new items and options for the new order
    ->reuse('items.product', 'customer') // link to the existing products and customer
    ->skip('payments')
    ->with(['status' => 'draft'])
    ->replicate();                      // one transaction

$copy = $result->model();               // the new, persisted order
$result->map()->get($oldItem);          // the copy of an original item
$result->stats()->copiedModels();       // root + items + options

Or add the trait to the model and start from the model itself; it returns the same builder:

use Pharaonic\Laravel\DeepReplicate\Concerns\DeepReplicates;

class Order extends Model
{
    use DeepReplicates;
}

$result = $order->deepReplicate('items', 'items.options')
    ->reuse('items.product', 'customer')
    ->replicate();

Laravel's own replicate() is untouched. At least one relation must always be selected.

Documentation

Full documentation: Deep Replicate Documentation.

Support & Security

See SUPPORT.md and SECURITY.md. Contributions are welcome; see CONTRIBUTING.md.

License

MIT. See LICENSE.