Recursive cloning and persistence of Laravel Eloquent models
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
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, (*2)
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 for details., (*3)
Structure
src/
tests/
vendor/
Version Compatibility
Laravel |
Cloner |
PHP |
5.4.x |
0.1.0 |
^5.4 |
6.x |
0.2.0 |
^7.3 |
7.x |
0.2.0 |
^7.3 |
8.x |
0.2.0 |
^7.3 |
9.x |
0.4.0 |
^8.0.2 |
Install
Via Composer, (*4)
``` bash
$ composer require anfischer/cloner, (*5)
The package will automatically register its service provider.
## Usage
### Basic Usage
``` php
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
Cloner also exposes a convinience method for cloning and persisting at the same time:, (*6)
``` php
$cloner = new Cloner(new CloneService, new PersistenceService);
$persistedModel = $cloner->cloneAndPersist($someEloquentModel);, (*7)
### 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.
``` php
$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
To publish the config file to config/cloner.php run:, (*8)
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
., (*9)
For example, (*10)
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
Please see CHANGELOG for more information on what has changed recently., (*11)
Testing
bash
$ composer test
, (*12)
Security
If you discover any security related issues, please email kontakt@season.dk instead of using the issue tracker., (*13)
Credits
License
The MIT License (MIT). Please see License File for more information., (*14)