- How do I install gabrielesbaiz/duplicate-toolkit in a Laravel app?
- Install it with Composer using composer require gabrielesbaiz/duplicate-toolkit. The package ships a service provider that is registered through Laravel auto-discovery, so no manual provider entry is needed. After that, add the HasDuplicates trait to each model you want to be duplicable.
- What PHP and Laravel versions does duplicate-toolkit require?
- Version 2.x requires PHP 8.3 or newer and Laravel 12 or 13. That is a hard gate, so check your composer.json before adding the package to an older application. CI is run across PHP 8.3 and 8.4 combined with Laravel 12 and 13, on both prefer-lowest and prefer-stable dependency sets.
- How do I duplicate an Eloquent model with all of its relationships?
- Add the Concerns\HasDuplicates trait to the model and call duplicate() on an instance. The package walks the model's relation graph, creates the copy, and persists it with the related records according to your configured strategies. You can optionally define a duplicateOptions() method on the model to declare default behavior for that model.
- Which relationship types can duplicate-toolkit copy?
- The traversal engine covers the standard Eloquent relation types, including morphToMany, and the test suite exercises soft deletes, UUID primary keys, and cyclic relation graphs against a real Testbench application. Relation discovery is reflection-based rather than method-calling, which avoids accessor side effects. Specialized cases such as media-library rows are handled by a dedicated pass rather than the generic walk, so adding support for a bespoke relation type is a bounded change.
- How do I choose which relations get copied, referenced, or skipped?
- Each relation supports three strategies: copy, reference, or skip. You can set them at the model level in duplicateOptions(), override them at the call site, or target nested relations using dot notation several levels deep. That lets you encode rules like copying an invoice, referencing the customer, and skipping audit notes in one call.
- How do I change column values on the duplicated record, like adding a suffix to the name?
- Use the attribute override helpers: suffix, prefix, replace, withAttributes, and mutate. Overrides are applied before the insert, so renaming a copy is a single write rather than an update after creation. Because the values are set pre-insert, the copy does not re-fire events that quiet() suppressed.
- Why does duplicate-toolkit only duplicate one level deep by default?
- Version 2.0 ships with max_depth set to 1, a deliberately conservative default that prevents unexpectedly large duplication runs. You can raise the depth per call or in duplicateOptions() when your relation graph warrants recursion. Use preview() and strictDepth() to confirm exactly what will be traversed before you commit to a deeper run.
- Can I preview a duplication before anything is written to the database?
- Yes. The package provides preview() and a dry-run mode so you can inspect the planned copies without persisting them. It also registers Artisan commands for relations, duplicate, make-options, and upgrade. Together these make it cheap to spike the package against two or three real models during evaluation.
- How do I upgrade from duplicate-toolkit 1.x to 2.0?
- Treat 2.0 as a rewrite rather than a patch, because it shares no code with 1.x. DuplicateOptions is now immutable and fully typed with __get() removed, so any 1.x code reading dynamic properties will break and needs updating. The package ships an upgrade Artisan command, and the maintainers note that 1.x results are suspect wherever a single request duplicated two different model classes, so plan a data-verification pass rather than a pure dependency bump.
- Is duplicate-toolkit production-ready and well maintained?
- The engineering hygiene is strong: PHPStan at max level with no baseline or ignores, Pint, Rector, around 105 tests covering every relation type, and a CI matrix across PHP and Laravel versions. The caveat is maturity rather than code quality, since the project has few stars, no dependents, and a single maintainer, so the 2.x line has no production track record yet. Duplication writes data, and correctness bugs there are silent shape defects rather than crashes, so validate it against your own models before relying on it in critical paths.