https://github.com/qtsolv/laravel-locking
Easily implement optimistic Eloquent model locking feature to your Laravel app.
https://github.com/qtsolv/laravel-locking
Last synced: 24 days ago
JSON representation
Easily implement optimistic Eloquent model locking feature to your Laravel app.
- Host: GitHub
- URL: https://github.com/qtsolv/laravel-locking
- Owner: qtsolv
- License: mit
- Created: 2022-06-21T08:14:28.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-17T17:05:43.000Z (almost 3 years ago)
- Last Synced: 2024-09-15T12:46:42.309Z (8 months ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# quarks/laravel-locking
Easily implement optimistic [Eloquent](https://laravel.com/docs/6.x/eloquent) model locking feature to your [Laravel](https://laravel.com/) app.
[![Latest Version][latest-version-image]][latest-version-url]
[![Downloads][downloads-image]][downloads-url]
[![PHP Version][php-version-image]][php-version-url]
[![License][license-image]](LICENSE)### Installation
```bash
composer require quarks/laravel-locking
```### Usage
In your migration classes, add the version column to your table as below:
```php
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('blog_posts', function (Blueprint $table) {
// create column for version tracking
$table->lockVersion();
// or to use a custom column name e.g., lock_version
$table->lockVersion('lock_version');
});
}/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('blog_posts', function (Blueprint $table) {
$table->dropLockVersion(); // or $table->dropLockVersion('lock_version');
});
}
```Then add the `LocksVersion` trait your model classes as follows:
```php
namespace App\Models;use Illuminate\Database\Eloquent\Model;
use Quarks\Laravel\Locking\LocksVersion;class BlogPost extends Model
{
use LocksVersion;
/**
* Override the default lock version column name, optional.
*/
protected static function lockVersionColumnName()
{
return 'lock_version';
}
}
```In your blade templates, include the current lock version as part of the form using the `lockInput` directive as below:
```html
@lockInput($blogPost)
```
In your controllers, fill the lock version from request using below helper:
```php
namespace App\Http\Controllers;use Quarks\Laravel\Locking\LockedVersionMismatchException;
// ... other imports
class BlogPostController extends Controller
{// ... more methods
public function update(BlogPost $blogPost, BlogPostRequest $request)
{
$data = $request->validated();
$blogPost->fill($data);
$blogPost->fillLockVersion();try {
$blogPost->save();
} catch (LockedVersionMismatchException $e) {
abort(409, 'This model was already modified elsewhere.');
}
}
}
```Your model update can now be simply protected from concurrent updates as shown above.
### License
See [LICENSE](LICENSE) file.
[latest-version-image]: https://img.shields.io/github/release/qtsolv/laravel-locking.svg?style=flat-square
[latest-version-url]: https://github.com/qtsolv/laravel-locking/releases
[downloads-image]: https://img.shields.io/packagist/dt/quarks/laravel-locking.svg?style=flat-square
[downloads-url]: https://packagist.org/packages/quarks/laravel-locking
[php-version-image]: http://img.shields.io/badge/php-7.2+-8892be.svg?style=flat-square
[php-version-url]: https://www.php.net/downloads
[license-image]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square