Coder Social home page Coder Social logo

laravel-duplicate's Introduction

Package discontinued! Check out VARBOX.IO instead.

Unfortunately this package is now discontinued.
Please check out Varbox (Laravel Admin Panel) for this functionality and much more.

Thank you!


Duplicate any Eloquent model along with its relationships

Build Status StyleCI Scrutinizer Code Quality

Overview

This package allows you to duplicate any Eloquent model record along with its underlying relationships.

Relationship types that can and will make sense to be duplicated: hasOne, morphOne, hasMany, morphMany, belongsToMany, morphToMany

Installation

Install the package via Composer:

composer require neurony/laravel-duplicate

Usage

Step 1

Your Eloquent models should use the Neurony\Duplicate\Traits\HasDuplicates trait and the Neurony\Duplicate\Options\DuplicateOptions class.

The trait contains an abstract method getDuplicateOptions() that you must implement yourself.

Here's an example of how to implement the trait:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Neurony\Duplicate\Options\DuplicateOptions;
use Neurony\Duplicate\Traits\HasDuplicates;

class YourModel extends Model
{
    use HasDuplicates;
    
    /**
     * Get the options for duplicating the model.
     *
     * @return DuplicateOptions
     */
    public function getDuplicateOptions(): DuplicateOptions
    {
        return DuplicateOptions::instance();
    }
}
Step 2

Once you've used the Neurony\Duplicate\Traits\HasDuplicates trait in your Eloquent models, you can duplicate model records by using the saveAsDuplicate() method present on that trait.

$model = YourModel::find($id);

$duplicatedModel = $model->saveAsDuplicate(); // returns the newly duplicated model instance

Customisations

Exclude certain columns

When duplicating a model, you can exclude certain columns from being duplicated by using the excludeColumns() method in your definition of the getDuplicateOptions() method.

The fields specified in the excludeColumns() method will be saved with their default value (null, false, 0, etc.)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->excludeColumns('column_one', 'column_two');
}
Specify unique columns

When duplicating a model, you can save certain columns in an unique format by using the uniqueColumns() method in your definition of the getDuplicateOptions() method.

The fields specified in the uniqueColumns() method will be saved in a unique format by appending (n) at the end.
Example: original name (1), original name (2)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->uniqueColumns('column_one', 'column_two');
}
Exclude entire relations

By default, when duplicating a model, all of its "child" relations (see Overview) are also duplicated along with it.

You can exclude certain relations from being duplicated by using the excludeRelations() method in your definition of the getDuplicateOptions() method.

The relations specified in the excludeRelations() method will not be duplicated along with the targeted model, meaning that the newly duplicated model will not have any records associated to it for the specified relations.

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->excludeRelations('relationOne', 'relationTwo');
}
Exclude certain columns from certain relations

When duplicating a model, you can exclude certain columns of its "child" relations from being duplicated by using the excludeRelationColumns() method in your definition of the getDuplicateOptions() method.

This method accepts only one parameter which should be an associative array containing:
key -> the name of a relation
value -> an array containing the columns to exclude for that relation

The fields specified in the excludeRelationColumns() method will be saved with their default value (null, false, 0, etc.)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->excludeRelationColumns([
            'relationOne' => ['column_one', 'column_two'],
            'relationTwo' => ['column_one'],
        ]);
}
Specify unique columns for certain relations

When duplicating a model, you can save certain columns of its "child" relations in an unique format by using the uniqueRelationColumns() method in your definition of the getDuplicateOptions() method.

This method accepts only one parameter which should be an associative array containing:
key -> the name of a relation
value -> an array containing the unique columns for that relation

The fields specified in the uniqueRelationColumns() method will be saved in an unique format by appending (n) at the end.
Example: original relation name (1), original relation name (2)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->uniqueRelationColumns([
            'relationOne' => ['column_one', 'column_two'],
            'relationTwo' => ['column_one'],
        ]);
}
Duplicate only the targeted model

If you only want to duplicate your targeted model without duplicating any relations whatsoever, you can specify this by using the disableDeepDuplication() method in your definition of the getDuplicateOptions() method.

When using this method, all relations of all types will be ignored when duplicating the model.

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->disableDeepDuplication();
}

Events

The duplicate functionality comes packed with two Eloquent events: duplicating and duplicated

You can implement these events in your Eloquent models as you would implement any other Eloquent events that come with the Laravel framework.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Neurony\Duplicate\Options\DuplicateOptions;
use Neurony\Duplicate\Traits\HasDuplicates;

class YourModel extends Model
{
    use HasDuplicates;

    /**
     * Boot the model.
     *
     * @return DuplicateOptions
     */
    public static function boot()
    {
        parent::boot();

        static::duplicating(function ($model) {
            // your logic here
        });

        static::duplicated(function ($model) {
            // your logic here
        });
    }
    
    /**
     * Get the options for duplicating the model.
     *
     * @return DuplicateOptions
     */
    public function getDuplicateOptions(): DuplicateOptions
    {
        return DuplicateOptions::instance();
    }
}

Credits

Security

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

License

The MIT License (MIT). Please see LICENSE for more information.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

laravel-duplicate's People

Contributors

bacanu avatar zbiller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

laravel-duplicate's Issues

Include only relations

Hello.

I like to suggest you to create a way to include only specified relations, so I can specify that I wants to duplicate user, for instance, with phones and addresses only. Even for future new relations, it will just be added to duplication rule if you manually set it.

Thanks!

It throws error when using $withCount = ['relation'] on child models

I have a $withCount = ['downloads'] in my child model, and when I try to replicate the parent model, it tries to replicate the child model by adding an extra column to the model named downloads_count. here's the error

message": "SQLSTATE[HY000]: General error: 1 table resources has no column named downloads_count (SQL: insert into "resources" ("lesson_id", "type", "title", "url", "deleted_at", "course_id", "downloads_count") values (1, external_resource, Illo aut nisi vitae ut a aut laborum ut., https://www.youtube.com/watch?v=bTqVqk7FSmY, ?, 2, 0))

I have fixed this by adding this line to my duplicateOptions

    /**
     * Get the options for duplicating the model.
     *
     * @return DuplicateOptions
     */
    public function getDuplicateOptions(): DuplicateOptions
    {
        return DuplicateOptions::instance()
            ->excludeRelationColumns(['resources' => ['downloads_count']]);
    }

Problem with not-null contrains in postgres

I get an error when I try to duplicate my model with columns with not-null contrains in postgres:

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "column_name" violates not-null constraint (SQL: insert into "table_name" DEFAULT VALUES returning id)

Cannot duplicate different model instances

Hello,

It seems that I cannot duplicate different model instances. Let's assume we have the following code:

$trackModel->saveAsDuplicate();
$imageModel->saveAsDuplicate();

The last line fails because it tries to also duplicate $trackModel's relationships, which is not correct. This mainly happens because in the RelationHelper class, the relations are appended to a static property.

Numa' bine ;)

Laravel 8 Support

Hello, will you be updating this package to support Laravel 8?

Thanks!

Support for Laravel 7

I've seen there is a pull request from Mar 4 2020 but still no answer. We are trying to upgrade to Laravel 7 but it's not possible atm due to this package.

Laravel 6.x

Hi,

Awesome package :)
Will you make this package Laravel 6.x compatible?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.