kevjo/laravel-collab
Composer 安装命令:
composer require kevjo/laravel-collab
包简介
Real-time collaborative editing for Laravel with intelligent locking and conflict resolution.
关键字:
README 文档
README
Pessimistic locking for Laravel Eloquent models. Prevent multiple users from editing the same record simultaneously.
Requirements
- PHP 8.5+
- Laravel 12+
- MySQL 8+ or PostgreSQL (required for row-level locking via
lockForUpdate())
Note: SQLite works for development/testing but does not support row-level locking. Race condition protection requires MySQL or PostgreSQL in production.
Installation
composer require kevjo/laravel-collab
Run the install command:
php artisan collab:install
This publishes the config file, migrations, and runs the migrations.
Quick Start
1. Add the Trait to Your Model
use Kevjo\LaravelCollab\Traits\HasConcurrentEditing; class Post extends Model { use HasConcurrentEditing; }
2. Acquire and Release Locks
// Acquire a lock $result = $post->acquireLock(auth()->user()); if ($result->isFailed()) { return back()->with('error', "This post is being edited by {$result->getLockedBy()->name}" ); } // Release a lock $post->releaseLock(auth()->user()); // Lock is also auto-released after model update (configurable) $post->update($request->validated());
3. Check Lock Status
$post->isLocked(); // Is it locked by anyone? $post->isLockedByUser(auth()->user()); // Is it locked by me? $post->isLockedByAnother(auth()->user()); // Is it locked by someone else? $post->lockOwner(); // Get the User who holds the lock $post->lockExpiresAt(); // Carbon instance of expiration $post->lockRemainingTime(); // Seconds until expiration
Middleware
The package provides a collab.lock middleware that returns HTTP 423 (Locked) when a route-bound model is locked by another user.
// Specify which route parameter to check Route::put('/posts/{post}', [PostController::class, 'update']) ->middleware('collab.lock:post'); // Auto-detect all lockable models on the route Route::put('/posts/{post}', [PostController::class, 'update']) ->middleware('collab.lock');
The middleware:
- Returns 423 Locked with lock info if the model is locked by another user
- Passes through if the model is unlocked or locked by the current user
- Skips the check entirely if no user is authenticated
Configuration
Publish the config:
php artisan vendor:publish --tag=collab-config
// config/collab.php return [ 'default_strategy' => 'pessimistic', 'lock_duration' => [ 'default' => 3600, // 1 hour 'min' => 60, // 1 minute minimum 'max' => 86400, // 24 hours maximum ], 'auto_release_after_update' => true, // Release lock when model is updated 'prevent_update_if_locked' => true, // Throw exception if locked by another 'tables' => [ 'locks' => 'model_locks', 'history' => 'model_lock_history', ], 'history' => [ 'enabled' => true, 'retention_days' => 30, ], ];
Lock Options
// Custom duration $post->acquireLock($user, ['duration' => 600]); // 10 minutes // Field-level locking $post->acquireLock($user, ['fields' => ['title', 'content']]); // Check field-level locks $post->isFieldLocked('title'); // true $post->getLockedFields(); // ['title', 'content'] // Custom metadata $post->acquireLock($user, ['metadata' => ['reason' => 'bulk update']]);
Lock Management
// Extend a lock $post->extendLock(1800, $user); // 30 more minutes // Force release (admin use) $post->forceReleaseLock(); // Request lock from owner (fires LockRequested event) $post->requestLock($requester); // Get structured lock info for API responses $post->getLockInfo(); // Returns: ['is_locked' => true, 'locked_by' => [...], 'expires_at' => '...', ...] $post->getLockStatus($user); // Returns: ['is_locked' => true, 'can_edit' => false, 'is_owner' => false, ...]
Facade
The Collab facade provides system-wide lock management:
use Kevjo\LaravelCollab\Facades\Collab; // Query locks Collab::activeLocks(); Collab::expiredLocks(); Collab::getLocksFor($post); Collab::getActiveLockFor($post); Collab::isModelLocked(Post::class, 1); Collab::getLocksForModelType(Post::class); // Bulk operations Collab::releaseAllLocksForUser($userId); Collab::releaseAllLocks(); // Cleanup Collab::cleanupExpiredLocks(); Collab::cleanupOldHistory(); Collab::runCleanup(); // History Collab::getHistoryFor($post); Collab::getUserHistory($userId); // Stats Collab::getStatistics();
Events
All events are in the Kevjo\LaravelCollab\Events namespace:
| Event | Fired When | Properties |
|---|---|---|
LockAcquired |
Lock is successfully acquired | $model, $lock, $user |
LockReleased |
Lock is released by owner | $model, $user |
LockForceReleased |
Lock is force-released (admin) | $model, $lockOwner, $releasedBy |
LockRequested |
User requests lock from owner | $model, $requester, $lockOwner |
LockExpired |
Expired lock is cleaned up | $model, $lock |
Listen to events in your EventServiceProvider or with closures:
use Kevjo\LaravelCollab\Events\LockRequested; Event::listen(LockRequested::class, function (LockRequested $event) { $event->lockOwner->notify(new LockRequestNotification( $event->requester, $event->model )); });
Artisan Commands
# Clean up expired locks php artisan collab:cleanup # Clean up expired locks + old history php artisan collab:cleanup --all # Preview what would be deleted php artisan collab:cleanup --dry-run # Install the package php artisan collab:install
Add to your scheduler for automatic cleanup:
// app/Console/Kernel.php $schedule->command('collab:cleanup')->hourly();
Automatic Behaviors
The trait hooks into Eloquent model events:
- Before update: If
prevent_update_if_lockedistrueand the model is locked by another user, aModelLockedException(HTTP 423) is thrown. - After update: If
auto_release_after_updateistrue, the lock is automatically released. - On delete: All locks on the model are released with history entries created.
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.
kevjo/laravel-collab 适用场景与选型建议
kevjo/laravel-collab 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「real-time」 「locking」 「laravel」 「collaboration」 「laravel12」 「php85」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kevjo/laravel-collab 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kevjo/laravel-collab 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kevjo/laravel-collab 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Talk is a Laravel based realtime messaging, chatting and conversation system. It helps to develop users messaging, chatting and conversations in easy way.
Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking
Wrapper for shared memory and locking functionality across different PHP extensions.
A Symfony bundle for Mutex implementation for PHP
Lock a list of object of the same entity to be executed by a single processor. When the lock is aquire, the lock ID generated can be use by a processor to execute action on that locked list.
Symfony Lock Component
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-16