Download the PHP package bfg/eloquent-duplicate without Composer
On this page you can find all versions of the php package bfg/eloquent-duplicate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package eloquent-duplicate
This is fork from Bfg/eloquent-duplicate to make it works with new Laravel 8.x|9.x
Duplicate any Eloquent model along with its relationships
- Overview
- Installation
- Usage
- Customisations
- Events
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:
Usage
Step 1
Your Eloquent models should use the Bfg\Duplicate\Traits\HasDuplicates
trait and the Bfg\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:
Step 2
Once you've used the Bfg\Duplicate\Traits\HasDuplicates
trait in your Eloquent models, you can duplicate model records by using the saveAsDuplicate()
method present on that trait.
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.)
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)
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.
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.)
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)
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.
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.
Credits
- BFG
- All Contributors
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.
All versions of eloquent-duplicate with dependencies
illuminate/contracts Version ^8.0 || ^9.0
illuminate/support Version ^8.0 || ^9.0
illuminate/database Version ^8.0 || ^9.0