- How does Laravel Rewind differ from Laravel Model Observers or Eloquent Events for tracking changes?
- Rewind goes beyond basic event logging by storing full version history with diffs and snapshots, enabling time-travel queries (e.g., `goTo($model, 3)`), restores, and diff comparisons. Observers only trigger events without persistent history or state reconstruction.
- Can I use Laravel Rewind with Laravel 10/11 and PHP 8.1+?
- Yes, Rewind is officially tested with Laravel 10/11 and requires PHP 8.1+. It integrates seamlessly with Eloquent’s event system, which remains stable across these versions. No breaking changes are expected for minor Laravel updates.
- What’s the best way to handle high-write workloads (e.g., 10K+ updates/hour) without performance issues?
- Enable queued versioning by setting `listener_should_queue=true` in your model’s `rewindStateFields`. This offloads version creation to Laravel queues (Redis/database), reducing lock contention. Monitor queue backlog and adjust snapshot intervals (e.g., `snapshot_interval=10` for critical models).
- How do I backfill existing records with version history?
- Use the `initVersion()` method to create an initial snapshot for existing records. For large tables, batch-process records via queue jobs to avoid timeouts. Example: `Model::chunk(100, fn($records) => $records->each(fn($model) => $model->initVersion()));`
- Does Rewind support soft deletes, and how are deleted states tracked?
- Yes, Rewind tracks `deleted_at` changes like any other field. When a model is soft-deleted, the version captures the `deleted_at` timestamp and state. You can query or restore from deleted states using `Rewind::goTo($model, $version)` or `Rewind::restore($model, $version)`.
- What’s the storage impact of using diffs vs. snapshots, and how do I optimize it?
- Diffs are storage-efficient but slower to reconstruct; snapshots are faster but larger. Configure `snapshot_interval` (e.g., `5` for critical models) to balance this. Use `Rewind::prune()` to automatically remove old versions (e.g., `--keep=100 --days=365`). Monitor the `rewind_versions` table size.
- Can I query historical states (e.g., ‘show me all orders with status=shipped on 2023-01-01’)?
- Yes, use `Rewind::goTo($model, $version)` to load a model’s state at a specific version, then query as usual. For bulk queries, filter by `created_at` in the `rewind_versions` table or use `Rewind::diff($model, $version1, $version2)` to compare states.
- How do I implement versioning for polymorphic relationships (e.g., a `Comment` model belonging to `Post` or `Video`)?
- Rewind supports polymorphic relationships natively. Ensure your model uses Eloquent’s polymorphic syntax (e.g., `public function commentable() { return $this->morphTo(); }`). Versioning will automatically track changes to the relationship’s foreign keys and owner IDs.
- Are there alternatives to Laravel Rewind for versioning Eloquent models?
- Alternatives include `spatie/laravel-activitylog` (event-based, no time-travel), `laravel-model-versions` (snapshot-only), or custom solutions with triggers. Rewind stands out with hybrid diff/snapshot storage, thread-safe locks, and queryable history—ideal for compliance or workflow-heavy apps.
- How do I secure version access (e.g., prevent users from restoring old versions they shouldn’t see)?
- Rewind doesn’t natively support role-based version access, but you can gate `Rewind::restore()` or `Rewind::goTo()` calls via middleware or policy checks. Example: `if (!auth()->user()->can('rewind_model', $model)) abort(403);`. Combine with Laravel’s authorization system.