Prevents update conflicts using the optimistic lock pattern in Laravel
laravel-enso/versions is a Laravel package for prevents update conflicts using the optimistic lock pattern in laravel.
It currently has 0 GitHub stars and 9.977 downloads on Packagist (latest version 2.0.6).
Install it with composer require laravel-enso/versions.
Discover more Laravel packages by laravel-enso
or browse all Laravel packages to compare alternatives.
Last updated
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.
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.
1 when creating a model.ConflictHttpException when another process already changed the record.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.
LaravelEnso\\Versions\\Traits\\VersionsMain methods:
checkVersion(?int $version = null): voidLifecycle hooks:
bootVersions()LaravelEnso\\Versions\\Exceptions\\VersionFactory method:
Version::recordModified(string $class)This package has no Enso package dependencies and can be used independently.
are welcome. Pull requests are great, but issues are good too.
Thank you to all the people who already contributed to Enso!