Laravel EnsoLaravel Enso
Guide
Back End
Front End
GitHub
Guide
Back End
Front End
GitHub
  • Back End

    • Action Logger
    • Addresses
    • Algolia
    • Algolia Webshop
    • API
    • Audits
    • Avatars
    • Cache Chain
    • Calendar
    • Cargo Partner
    • Categories
    • Charts
    • CI/CD
    • Cli
    • CNP Validator
    • Comments
    • Commercial
    • Companies
    • Core
    • Countries
    • Currencies
    • Data Export
    • Data Import
    • Discounts
    • Documents
    • Dynamic Methods
    • EAV
    • Emag
    • Enums
    • Excel
    • Facebook
    • Files
    • Filters
    • Financials
    • Forms
    • Frisbo
    • Google
    • Helpers
    • Holidays
    • How-to
    • Image Transformer
    • Impersonate
    • Interactions
    • Inventory
    • IO
    • Localisation
    • Lockable Models
    • Logs
    • Measurement Units
    • Meili Search
    • Meili Search Webshop
    • Menus
    • Migrator
    • Monitored Emails
    • Notifications
    • Packaging Units
    • PDF
    • People
    • Permissions
    • Product Eav
    • Product Lots
    • Products
    • Questionnaires
    • Rememberable
    • Roles
    • Sale Channels
    • Searchable
    • Select
    • Sentry
    • Services
    • Smart Bill
    • Sms Advert
    • Stripe
    • Tables
    • Tasks
    • Teams
    • Ticketing
    • Track Who
    • Tutorials
    • Typesense
    • Typesense Webshop
    • Unit Conversion
    • Upgrade
    • UPS
    • User Groups
    • Users
    • Versions
    • Virtual Call Center
    • Vouchers
    • Webshop
    • Webshop Commercial

Versions

LicenseStableDownloadsPHPIssuesMerge Requests

Description

Versions prevents concurrent-update conflicts through an optimistic-lock field on Eloquent models.

The package increments a version column on each update, checks the persisted version under a database lock before writing, and throws an HTTP 409 conflict when the record was modified after it was loaded.

It can work independently of the Laravel Enso ecosystem.

Installation

Install the package:

composer require laravel-enso/versions

Add the trait to a model that has a version column:

use Illuminate\Database\Eloquent\Model;
use LaravelEnso\Versions\Traits\Versions;

class Invoice extends Model
{
    use Versions;
}

The trait expects a version integer column by default. To use another column name, define a protected $versioningAttribute property on the model.

Features

  • Sets the version column to 1 when creating a model.
  • Starts a database transaction before updates and checks the persisted version under a lock.
  • Increments the version automatically when the update is accepted.
  • Throws a ConflictHttpException when another process already changed the record.

Usage

Manual version check:

$invoice->checkVersion($request->integer('version'));

Custom version column:

class Invoice extends Model
{
    use Versions;

    protected string $versioningAttribute = 'lock_version';
}

If the check fails, the package throws LaravelEnso\\Versions\\Exceptions\\Version, which extends Symfony's ConflictHttpException.

API

Trait

  • LaravelEnso\\Versions\\Traits\\Versions

Main methods:

  • checkVersion(?int $version = null): void

Lifecycle hooks:

  • bootVersions()

Exception

  • LaravelEnso\\Versions\\Exceptions\\Version

Factory method:

  • Version::recordModified(string $class)

Depends On

This package has no Enso package dependencies and can be used independently.

Contributions

are welcome. Pull requests are great, but issues are good too.

Thank you to all the people who already contributed to Enso!

License

MIT

Edit this page on GitHub
Last Updated: 4/21/2026, 8:24:43 AM
Prev
Users
Next
Virtual Call Center