Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Versionable Laravel Package

mpociot/versionable

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Model-Centric Versioning: The package is designed to integrate seamlessly with Laravel’s Eloquent ORM, making it ideal for applications where audit trails, rollback capabilities, or compliance tracking are critical (e.g., CMS, financial records, or regulatory systems).
  • Event-Driven Hooks: Leverages Laravel’s model events (saved, deleted, restored) to trigger versioning automatically, reducing boilerplate. Aligns well with event-sourced architectures or systems requiring immutable history.
  • Database Agnostic: Works with Laravel’s query builder, ensuring compatibility across MySQL, PostgreSQL, SQLite, etc., but relies on a separate versions table (potential schema complexity).
  • Soft Deletes: Supports Laravel’s soft deletes, enabling versioning of "deleted" records (useful for compliance or recovery).

Integration Feasibility

  • Low Coupling: Uses traits (Versionable) for model integration, avoiding tight coupling with core logic. Can be adopted incrementally (e.g., start with critical models).
  • Migration Path: Requires database migrations for the versions table, which may conflict with existing schemas. Publishable migrations allow customization (e.g., adding user_id for accountability).
  • Laravel Ecosystem Synergy: Plays well with:
    • Laravel Scout (if versioned models are searchable).
    • Laravel Nova (for UI-based version management).
    • Laravel Policies (to restrict version access by role).
  • Testing Overhead: Adds complexity to unit/integration tests due to versioning side effects (e.g., mocking version creation in tests).

Technical Risk

  • Performance Impact:
    • Write Operations: Each model save triggers version creation, increasing DB load. Mitigate with:
      • Batch processing for bulk updates.
      • Queueing version creation (e.g., queue:after events).
    • Read Operations: Querying versions (e.g., Version::find(100)) may require index optimization on versionable_id and version.
  • Data Bloat: Unbounded version history can inflate storage. Solutions:
    • TTL policies (e.g., purge versions older than 90 days).
    • Compression for large text fields (e.g., JSON blobs).
  • Concurrency Risks: Race conditions if multiple users edit the same model simultaneously. Mitigate with:
    • Optimistic locking (Laravel’s spatie/laravel-activitylog integration).
    • Database transactions for versioned operations.
  • Schema Lock-In: Custom migrations may require future-proofing (e.g., adding created_at to versions table for time-based queries).

Key Questions

  1. Use Case Alignment:
    • Is versioning needed for all models or only critical ones (e.g., user profiles, financial transactions)?
    • Will versions be used for auditing, rollbacks, or analytics?
  2. Scalability:
    • What’s the expected write volume? Can version creation be async?
    • How will version queries scale (e.g., paginated version history)?
  3. Compliance:
    • Are there retention policies (e.g., GDPR right to erasure)?
    • Does versioning need to be tamper-proof (e.g., cryptographic hashes)?
  4. UI/UX:
    • Will versions be exposed to end-users (e.g., "View history" button)?
    • Does the team need Laravel Nova or custom admin panels for version management?
  5. Alternatives:
    • Compare with spatie/laravel-activitylog (simpler, less feature-rich) or custom solutions (e.g., PostgreSQL jsonb snapshots).

Integration Approach

Stack Fit

  • Laravel Core: Native support for Eloquent models, events, and migrations. No framework-level changes required.
  • Database: Requires a relational DB (MySQL/PostgreSQL/SQLite). No support for NoSQL or document stores.
  • Testing: Compatible with PHPUnit, Pest, and Laravel’s testing helpers (e.g., assertSoftDeleted).
  • DevOps:
    • CI/CD: Migrations must run in pipelines (e.g., GitHub Actions).
    • Backups: Versions table should be included in DB backups.
  • Extensions:
    • APIs: Works with Laravel Sanctum/Passport for version access control.
    • Frontend: Can integrate with Inertia.js or Livewire for real-time version previews.

Migration Path

  1. Assessment Phase:
    • Audit models to identify versioning candidates (prioritize by business impact).
    • Design the versions table schema (e.g., add user_id, ip_address, or metadata columns).
  2. Pilot Phase:
    • Start with non-critical models (e.g., blog posts) to test performance and workflows.
    • Implement basic versioning (no rollbacks yet).
  3. Full Rollout:
    • Apply use Versionable trait to target models.
    • Run migrations (publish and customize if needed).
    • Add version-aware logic (e.g., previousVersion()->revert() in controllers).
  4. Optimization:
    • Add indices to versions table for performance.
    • Implement queued versioning for high-traffic models.
    • Build admin tools for version management (e.g., Nova resource).

Compatibility

  • Laravel Versions: Officially supports L4–L11, but test thoroughly on L10/L11 for edge cases (e.g., new Eloquent features).
  • PHP Versions: Requires PHP 8.0+ (check for deprecations in older versions).
  • Third-Party Conflicts:
    • Model Observers: May interfere with versioning events. Use withoutEvents() for bulk operations.
    • Serializers: Ensure Version models are handled correctly by APIs (e.g., spatie/laravel-fractal).
  • Legacy Systems: If using custom model events, ensure they don’t clash with Versionable hooks.

Sequencing

  1. Pre-Requirements:
    • Upgrade Laravel/PHP to supported versions.
    • Backup the database before migrations.
  2. Core Integration:
    • Install package: composer require mpociot/versionable.
    • Publish migrations: php artisan vendor:publish --tag=versionable-migrations.
    • Customize migrations (e.g., add user_id).
    • Run migrations: php artisan migrate.
  3. Model-Level Setup:
    • Add use Mpociot\Versionable\Traits\Versionable; to target models.
    • Configure versionable() method if needed (e.g., versionable()->withMetadata()).
  4. Testing:
    • Write tests for version creation/rollback (e.g., assertModelHasVersion()).
    • Test edge cases (e.g., concurrent edits, soft deletes).
  5. Monitoring:
    • Add logging for version operations (e.g., VersionCreated events).
    • Set up alerts for version table growth.

Operational Impact

Maintenance

  • Package Updates:
    • Monitor mpociot/versionable for breaking changes (e.g., Laravel 11 compatibility).
    • Test updates in staging before production.
  • Customizations:
    • Overriding migrations or traits may require backporting fixes from upstream.
    • Document custom logic (e.g., VersionObserver extensions).
  • Deprecation:
    • Laravel 4 support may drop; plan for L5.5+ minimum in 2–3 years.

Support

  • Troubleshooting:
    • Common issues:
      • Missing versions: Check event listeners or observe() conflicts.
      • Performance: Optimize versions table queries (e.g., select * from versions where versionable_id = ? order by version desc limit 1).
      • Rollback failures: Ensure revert() is called in a transaction.
    • Debug with:
      \Mpociot\Versionable\Events\VersionCreated::dispatch($version);
      
  • Documentation:
    • Internal docs should cover:
      • Which models are versioned and why.
      • Rollback procedures (e.g., "How to revert a user profile").
      • Version retention policies.
  • User Training:
    • Train devs on:
      • Using previousVersion() vs. Version::find().
      • Handling version conflicts (e.g., "Model was edited by User B after you started editing").

Scaling

  • Database Scaling:
    • Read Replicas: Offload version queries to replicas.
    • Partitioning: Shard versions table by versionable_type if >10M
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony
spatie/flare-daemon-runtime