- How do I install bkwld/cloner in a Laravel app?
- Install it with composer require bkwld/cloner. On Laravel 5.5 and newer the service provider is auto-discovered, so no bootstrap wiring is required; on older versions register Bkwld\Cloner\ServiceProvider in config/app.php. Then add the Cloneable trait to each model you want to duplicate.
- Which Laravel and PHP versions does bkwld/cloner support?
- The README references Laravel 5.5 auto-discovery, but the package predates Laravel 10, 11, and 12. Because the trait overrides Eloquent's replicate behavior, framework internals matter. Check the composer.json constraints and CI matrix against your target Laravel and PHP versions before committing.
- How do I clone a model with its relationships using Cloner?
- Add use \Bkwld\Cloner\Cloneable to your Eloquent model and declare a protected $cloneable_relations array naming the relationship methods to follow. Calling duplicate() on an instance saves a new model and copies the configured related rows, including pivot rows for belongsToMany relationships. Relations not listed in $cloneable_relations are not cloned.
- Can bkwld/cloner clone files attached to a model?
- Yes, Cloner can clone files attached to a model when file cloning is configured, for example through $cloneable_files. Confirm whether your version uses Laravel's Storage facade and disks or raw local paths, because that determines whether S3, Spaces, and CDN-backed assets clone safely. Also note that file copying happens outside the database transaction.
- How do I duplicate a record to another database connection in Laravel?
- Call duplicateTo('connection') on the model instance, where connection is the Laravel database connection name. The package assumes schema parity between the source and target connections rather than validating it, so a mismatch may fail opaquely at insert time. Test the target schema before using it for tenant provisioning, seeding, or production duplication.
- Does bkwld/cloner fire model events on cloned records?
- Cloned models are saved through Eloquent, so normal model events such as created and saved fire on the clone. Listeners for audit logs, search indexing, mailers, or webhooks will run as if a user created the record. There is no documented event-suppression hook in the package context, so guard side effects in your listeners or check your installed version.
- Is cloning wrapped in a database transaction, and are file copies atomic?
- Do not assume the whole clone is atomic: the package context notes that file duplication happens outside the database transaction. A failure mid-clone can leave copied objects in storage with no owning row or produce a partially populated relation graph. For production use, consider deferring file copying to a queued job and verifying cleanup behavior.
- Does Cloner handle polymorphic, belongsToMany, and self-referencing relations?
- The README documents cloning hasMany relationships and belongsToMany relationships with new pivot rows. Polymorphic relations such as morphMany and morphTo, cyclic or self-referencing graphs, and deep nested graphs are the main risk areas. Verify recursion guards and graph correctness on a representative model before relying on Cloner for complex schemas.
- What happens with unique columns like slugs or emails when cloning?
- Cloner copies model attributes in the same way Eloquent replication does, so unique-constrained values such as slugs, emails, external IDs, and UUIDs can be carried over unchanged. Saving the clone can then fail on a unique index. Use model hooks or mutators to regenerate those values before the clone is saved.
- Is bkwld/cloner maintained and suitable for production?
- The package has a low ecosystem footprint, with roughly 488 GitHub stars and zero dependents reported, so it is closer to the frontier than a mainstream package. Maintenance is unclear from the context, so check release cadence and issue activity before adopting it broadly. It is reasonable for a low-risk model clone after you verify version support, transaction behavior, and event side effects; rollback is simply removing the trait.