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
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.
src/
tests/
vendor/
| 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 |
Via Composer
$ composer require anfischer/clonerThe package will automatically register its service provider.
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);Cloner also exposes a convinience method for cloning and persisting at the same time:
$cloner = new Cloner(new CloneService, new PersistenceService);
$persistedModel = $cloner->cloneAndPersist($someEloquentModel);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.
$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]];To publish the config file to config/cloner.php run:
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.
For example
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
]
];
Please see CHANGELOG for more information on what has changed recently.
$ composer testIf you discover any security related issues, please email kontakt@season.dk instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.