Eloquent Cloner Package

Eloquent Cloner Package

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

Here’s a basic example of a model using the Cloneable trait:

class Article extends Eloquent
{
    use \Bkwld\Cloner\Cloneable;
}

Here’s how you can clone a model instance, even to another database:

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

// Cloned to another database by connection name
$clone = Article::first()->duplicateTo('production');

A more advanced example includes defining which relationships cloned along with the model:

class Article extends Eloquent
{
    use \Bkwld\Cloner\Cloneable;

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

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

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

Check out the documentation for full details on how you can also define how to clone files attached to a model. You can learn more about this app and check out the source code on GitHub at BKWLD/cloner.


Filed in: Laravel Packages


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Full-Stack Developer
Tampa, FL / Remote
Nu Image Medical
PHP Developer
Remote
X-Team
Full-Stack Developer
Sioux Falls, SD or Remote
Electric Pulp
Senior PHP / Vue.js Developer
Portland, OR / Remote
Bright Oak
Full Stack Vue.js Developer (Front-end focus)
Remote
LenderHomePage.com
Senior Laravel Web Application Developer
NYC or Remote
Happy Cog
Laravel Developer (Vue, SQL Server)
Geneva, Switzerland / Remote
World Trade Organization