Download the PHP package reshadman/laravel-optimistic-locking without Composer

On this page you can find all versions of the php package reshadman/laravel-optimistic-locking. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-optimistic-locking

Laravel Optimistic Locking

Build Status

Adds optimistic locking feature to Eloquent models.

Installation

This package supports Laravel 5.5., 5.6., 5.7., 5.8., and 6.* .

Usage

Basic usage

use the \Reshadman\OptimisticLocking\OptimisticLocking trait in your model:

and add the integer lock_version field to the table of the model:

Then you are ready to go, if the same resource is edited by two different processes CONCURRENTLY then the following exception will be raised:

You should catch the above exception and act properly based on your business logic.

Maintaining lock_version during business transactions

You can keep track of a lock version during a business transaction by informing your API or HTML client about the current version:

and in controller:

So if two authors are editing the same content concurrently, you can keep track of your Read State, and ask the second author to rewrite his changes.

Disabling and enabling optimistic locking

You can disable and enable optimistic locking for a specific instance:

By default optimistic locking is enabled when you use OptimisticLocking trait in your model, to alter the default behaviour you can set the lock strictly to false:

and then you may enable it: $blogPost->enableLocking();

Use a different column for tracking version

By default the lock_version column is used for tracking version, you can alter that by overriding the following method of the trait:

What is optimistic locking?

For detailed explanation read the concurrency section of Patterns of Enterprise Application Architecture by Martin Fowler.

There are two way to approach generic concurrency race conditions:

  1. Do not allow other processes (or users) to read and update the same resource (Pessimistic Locking)
  2. Allow other processes to read the same resource concurrently, but do not allow further update, if one of the processes updated the resource before the others (Optimistic locking).

Laravel allows Pessimistic locking as described in the documentation, this package allows you to have Optimistic locking in a rails like way.

What happens during an optimistic lock?

Every time you perform an upsert action to your resource(model), the lock_version counter field in the table is incremented by 1, If you read a resource and another process updates the resource after you read it, the true version counter is incremented by one, If the current process attempts to update the model, simply a StaleModelLockingException will be thrown, and you should handle the race condition (merge, retry, ignore) based on your business logic. That is simply via adding the following criteria to the update query of a optimistically lockable model:

If the resource has been updated before your update attempt, then the above will simply update no records and it means that the model has been updated before current attempt or it has been deleted.

Why don't we use updated_at for tracking changes?

Because they may remain the same during two concurrent updates.

Running tests

Clone the repo, perform a composer install and run:

License

The MIT License (MIT). Please see License File for more information. ense (MIT). Please see License File for more information.


All versions of laravel-optimistic-locking with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1
illuminate/database Version ~5.5.0|~5.6.0|~5.7.0
illuminate/support Version ~5.5.0|~5.6.0|~5.7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package reshadman/laravel-optimistic-locking contains the following files

Loading the files please wait ....