- What is overtrue/laravel-versionable used for?
- It adds lightweight version history to Laravel Eloquent models. You can track changes to selected attributes, browse saved versions, and revert a model to any previous version. It is useful for audit trails, content rollbacks, and admin-panel comparisons.
- How do I install overtrue/laravel-versionable in a Laravel app?
- Install it with composer require overtrue/laravel-versionable. Then publish the package config and migrations with php artisan vendor:publish and run php artisan migrate. Add the Versionable trait to any Eloquent model you want to version.
- Which Laravel and PHP versions does laravel-versionable support?
- The package requires PHP 8.1 or newer and Laravel framework 9.0 or newer. The evaluation notes explicit support for Laravel 12 and 13, but you should pin a stable package version and check compatibility before upgrading major Laravel releases.
- How do I choose which attributes are versioned?
- Define a protected $versionable array to whitelist the attributes you want to track, or use protected $dontVersionable as a blacklist to exclude attributes. The package records only changed attributes by default with the DIFF strategy, which keeps storage smaller. Use the blacklist for models with many fields or sensitive columns.
- Does laravel-versionable store full snapshots or only changed fields?
- By default it uses the DIFF strategy and stores only changed attributes for each version. You can switch the strategy to SNAPSHOT if you need full attribute copies for simpler queries or complex objects. Diff output is handled with jfcherng/php-diff for readable comparisons.
- How do I revert a model to a previous version?
- Use the package APIs to fetch a version and revert the model, for example by retrieving a version with versionAt() or latestVersion and calling the revert method provided by the trait. For critical operations, wrap the revert in a database transaction. This helps avoid partial updates if related data also changes.
- How many versions does laravel-versionable keep, and can I limit them?
- The package supports keeping a specified number of versions, commonly configured through $keepVersions. You can use this to control version-table growth and retention. Combine retention limits with the DIFF strategy to reduce storage overhead on high-write models.
- Is laravel-versionable suitable for production and high-write applications?
- It is suitable for many Laravel applications, but every save on a versionable model triggers a version check, which adds write overhead. The evaluation estimates roughly 20 to 50 percent write overhead depending on usage. Use withoutVersion() where versioning is unnecessary and eager-load versions only when needed to limit read overhead.
- How can I test versioning logic with laravel-versionable?
- The package is compatible with PHPUnit and Pest and includes feature-test examples. Create a dedicated test model with the Versionable trait and assert that saves create versions only for changed attributes. Test edge cases such as JSON fields, nested arrays, timestamps, and revert behavior inside transactions.
- Are there alternatives to overtrue/laravel-versionable for model versioning?
- Alternatives include event-sourcing packages, audit-log packages such as spatie/laravel-activitylog in some use cases, and custom version tables. This package is a good fit when you want a minimalist snapshot or diff-based history without event sourcing. It also offers Filament integration through filament-versionable for admin-panel diffs.