PHPackages                             vdlp/eloquent-model-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. vdlp/eloquent-model-cloner

ActiveLibrary

vdlp/eloquent-model-cloner
==========================

Clone eloquent models and relations

1.1.0(4y ago)116.1k↓50%MITPHPPHP ^7.4 || ^8.0CI passing

Since Jul 29Pushed 4y ago4 watchersCompare

[ Source](https://github.com/vdlp/eloquent-model-cloner)[ Packagist](https://packagist.org/packages/vdlp/eloquent-model-cloner)[ Docs](https://github.com/vdlp/eloquent-model-cloner)[ RSS](/packages/vdlp-eloquent-model-cloner/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

Eloquent model cloner
=====================

[](#eloquent-model-cloner)

This package was inspired by  and modified to also work with OctoberCMS installations.

A trait for Laravel Eloquent models that lets you clone a model and it's relationships, including files. Even to another database.

Installation
------------

[](#installation)

To get started with the Eloquent Model Cloner, use Composer to add the package to your project's dependencies:

```
composer require vdlp/eloquent-model-cloner

```

Usage
-----

[](#usage)

Your model should now look like this:

```
class Article extends Eloquent {
   use \Vdlp\EloquentModelCloner\Cloneable;
}
```

You can clone an Article model like so:

```
$clone = Article::first()->duplicate();
```

### Cloning Relationships

[](#cloning-relationships)

Lets say your `Article` has many `Photos` (a one to many relationship) and can have more than one `Authors` (a many to many relationship). Now, your `Article` model should look like this:

```
class Article extends Eloquent {
   use \Vdlp\EloquentModelCloner\Cloneable;

   protected $cloneableRelations = ['photos', 'authors'];

   public function photos() {
       return $this->hasMany('Photo');
   }

   public function authors() {
        return $this->belongsToMany('Author');
   }
}
```

The `$cloneableRelations` informs the `Cloneable` as to which relations it should follow when cloning. Now when you call `Article::first()->duplicate()`, all of the `Photo` rows of the original will be copied and associated with the new `Article`. And new pivot rows will be created associating the new `Article` with the `Authors` of the original (because it is a many to many relationship, no new `Author` rows are created). Furthermore, if the `Photo` model has many of some other model, you can specify `$cloneableRelations` in its class and `Cloner` will continue replicating them as well.

### Customizing the cloned attributes

[](#customizing-the-cloned-attributes)

By default, `Cloner` does not copy the `id` (or whatever you've defined as the `key` for the model) field; it assumes a new value will be auto-incremented. It also does not copy the `created_at` or `updated_at`. You can add additional attributes to ignore as follows:

```
class Photo extends Eloquent {
   use \Vdlp\EloquentModelCloner\Cloneable;

   protected $cloneExemptAttributes = ['uid', 'source'];

   public function article() {
        return $this->belongsTo('Article');
   }

   public function onCloning($src, $child = null) {
        $this->uid = str_random();

        if ($child) {
            echo 'This was cloned as a relation!';
        }

        echo 'The original key is: ' . $src->getKey();
   }
}
```

The `$cloneExemptAttributes` adds to the defaults. If you want to replace the defaults altogether, override the trait's `getCloneExemptAttributes()` method and return an array.

Also, note the `onCloning()` method in the example. It is being used to make sure a unique column stays unique. The `Cloneable` trait adds to no-op callbacks that get called immediately before a model is saved during a duplication and immediately after: `onCloning()` and `onCloned()`. The `$child` parameter allows you to customize the behavior based on if it's being cloned as a relation or direct.

In addition, Cloner fires events during cloning and when the model has been cloned, see:

- `\Vdlp\EloquentModelCloner\Events\Cloned`
- `\Vdlp\EloquentModelCloner\Events\Cloning`

The event payload contains the clone and the original model instances.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~323 days

Total

2

Last Release

1796d ago

PHP version history (2 changes)1.0.0PHP ^7.4

1.1.0PHP ^7.4 || ^8.0

### Community

Maintainers

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

---

Top Contributors

[![adrenth](https://avatars.githubusercontent.com/u/5699988?v=4)](https://github.com/adrenth "adrenth (7 commits)")[![nathan-van-der-werf](https://avatars.githubusercontent.com/u/1123054?v=4)](https://github.com/nathan-van-der-werf "nathan-van-der-werf (7 commits)")

---

Tags

laraveltraitvdlpeloquent-model-cloner

### Embed Badge

![Health badge](/badges/vdlp-eloquent-model-cloner/health.svg)

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

PHPackages © 2026

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