PHPackages                             anfischer/cloner - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. anfischer/cloner

ActiveLibrary[Database &amp; ORM](/categories/database)

anfischer/cloner
================

A package which allows for easy recursive cloning and persistence of Laravel Eloquent models

v0.4.1(3y ago)22081[1 PRs](https://github.com/anfischer/cloner/pulls)MITPHPPHP &gt;=7.3

Since May 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/anfischer/cloner)[ Packagist](https://packagist.org/packages/anfischer/cloner)[ Docs](https://github.com/anfischer/cloner)[ RSS](/packages/anfischer-cloner/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (6)Dependencies (8)Versions (8)Used By (0)

Recursive cloning and persistence of Laravel Eloquent models
============================================================

[](#recursive-cloning-and-persistence-of-laravel-eloquent-models)

[![Latest Version on Packagist](https://camo.githubusercontent.com/492e93660aad18a84454e663cbe7729ade83c70b7b8a423cc1c95014ae33c907/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e666973636865722f636c6f6e65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/anfischer/cloner)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/05bae5beaed4b8b2fde4b613c46b723555461b69de40da564e8ad09e91fef3e0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f616e666973636865722f636c6f6e65722f4d61696e253230776f726b666c6f773f7374796c653d666c61742d737175617265)](https://github.com/anfischer/cloner/actions)[![Coverage Status](https://camo.githubusercontent.com/6ee891b130bb413074920a80e276c6f4fb60b91eed64ef8be3ebf6d5d4dc02e5/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f616e666973636865722f636c6f6e65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/anfischer/cloner/code-structure)[![Quality Score](https://camo.githubusercontent.com/d7c6b8bb895cab998c087833f5f07862554a5c257ab0ab5bd06f6e4fb5cec259/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f616e666973636865722f636c6f6e65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/anfischer/cloner)[![Total Downloads](https://camo.githubusercontent.com/b9f0abdc0525ebf3e5409c7aafc81e1451de75da05c6bb8c67079d240a72bfb1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e666973636865722f636c6f6e65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/anfischer/cloner)

A package which allows for easy recursive cloning and persistence of Laravel Eloquent models, including:

- Recursive cloning of Eloquent models and their relationships without forced persistence, allowing for in-memory changes to cloned models before they are saved to the database
- Persistence of recursive relationships including cloned pivot data

*Since this is a feature I commonly rely on in client projects, I decided to extract the functionality into a package. However this also has the consequence that your mileage may vary, and hence pull requests are welcome - please see [CONTRIBUTING](CONTRIBUTING.md) for details.*

Structure
---------

[](#structure)

```
src/
tests/
vendor/

```

Version Compatibility
---------------------

[](#version-compatibility)

LaravelClonerPHP5.4.x0.1.0^5.46.x0.2.0^7.37.x0.2.0^7.38.x0.2.0^7.39.x0.4.0^8.0.2Install
-------

[](#install)

Via Composer

```
$ composer require anfischer/cloner
```

The package will automatically register its service provider.

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use Anfischer\Cloner;

$clone = (new CloneService)->clone($someEloquentModel);
$persistedModel = (new PersistenceService)->persist($clone);

or

$cloner = new Cloner(new CloneService, new PersistenceService);
$clone = $cloner->clone($someEloquentModel);
$persistedModel = $cloner->persist($clone);

or

$clone = \Cloner::clone($someEloquentModel);
$persistedModel = \Cloner::persist($clone);
```

### Convenience Methods

[](#convenience-methods)

Cloner also exposes a convinience method for cloning and persisting at the same time:

```
$cloner = new Cloner(new CloneService, new PersistenceService);
$persistedModel = $cloner->cloneAndPersist($someEloquentModel);
```

### Cloned Model Map

[](#cloned-model-map)

You may wish to keep track of which models were cloned and the keys of their respective clones. In order to do this Cloner keeps a record of these keys.

```
$cloneService = new CloneService()

// $personModel->id === 1;
// gettype($personModel) === App\Person;

$clone = ($cloneService)->clone($personModel);

$persistedModel = (new PersistenceService)->persist($clone);
// or
$persistedModel = $clone->save();

// $persistedModel->id === 2

$map = $cloneService->getKeyMap();

// $map === [App\Person => [1 => 2]];
```

Configuration
-------------

[](#configuration)

To publish the config file to config/cloner.php run:

```
php artisan vendor:publish --provider="Anfischer\Cloner\ClonerServiceProvider"

```

Cloner supports various persistence strategies by default. These can be configured by modifying the configuration in `config/cloner.php`.

For example

```
return [

    'persistence_strategies' => [
        Illuminate\Database\Eloquent\Relations\HasOne::class =>
            Anfischer\Cloner\Strategies\PersistHasOneRelationStrategy::class,
        Illuminate\Database\Eloquent\Relations\HasMany::class =>
            Anfischer\Cloner\Strategies\PersistHasManyRelationStrategy::class,
        Illuminate\Database\Eloquent\Relations\BelongsToMany::class =>
            Anfischer\Cloner\Strategies\PersistBelongsToManyRelationStrategy::class,

        // You can add your own strategies for relations
        SomePackage\Relations\CustomRelation =>
            App\Cloner\PersistenceStrategies\PersistSomePackageCustomRelationStrategy
    ]
];

```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Andreas Fischer](https://github.com/anfischer)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~315 days

Recently: every ~170 days

Total

6

Last Release

1329d ago

PHP version history (2 changes)v0.1.0PHP &gt;=7.1

v0.2.0PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/ede147c0254f2a0e0fb9833c0871b6e86f21f71c53b9c35b35fbdaa1a03bc4ec?d=identicon)[anfischer](/maintainers/anfischer)

---

Top Contributors

[![benswinburne](https://avatars.githubusercontent.com/u/1208977?v=4)](https://github.com/benswinburne "benswinburne (52 commits)")[![anfischer](https://avatars.githubusercontent.com/u/2504884?v=4)](https://github.com/anfischer "anfischer (12 commits)")

---

Tags

cloneranfischer

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/anfischer-cloner/health.svg)

```
[![Health](https://phpackages.com/badges/anfischer-cloner/health.svg)](https://phpackages.com/packages/anfischer-cloner)
```

###  Alternatives

[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[aglipanci/laravel-eloquent-case

Adds CASE statement support to Laravel Query Builder.

115157.2k](/packages/aglipanci-laravel-eloquent-case)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
