jeromejhipolito/laravel-eloquent-atomic
Composer 安装命令:
composer require jeromejhipolito/laravel-eloquent-atomic
包简介
Atomic upsert operations with soft-delete awareness and pessimistic locking for Laravel Eloquent models.
README 文档
README
Atomic upsert operations with soft-delete awareness and pessimistic locking for Laravel Eloquent models.
Replaces Laravel's updateOrCreate() with a race-condition-safe alternative that uses SELECT ... FOR UPDATE with automatic retry on unique constraint violations and deadlocks. Soft-deleted records are automatically detected and restored.
Installation
composer require jeromejhipolito/laravel-eloquent-atomic
Requires PHP 8.2+ and Laravel 11 or 12.
Usage
Add the AtomicUpsert trait to any service class that needs race-condition-safe upserts:
use JeromeJHipolito\EloquentAtomic\Traits\AtomicUpsert; class SubscriptionService { use AtomicUpsert; public function syncSubscription(array $data): Subscription { return $this->atomicUpdateOrCreate( Subscription::class, ['external_id' => $data['id']], // lookup attributes (must have unique index) ['status' => $data['status'], 'name' => $data['name']] // values to set ); } }
How It Works
- Starts a database transaction
- Attempts
SELECT ... FOR UPDATEto find an existing record (including soft-deleted) - If found: restores (if soft-deleted) and updates in a single query
- If not found: creates the record
- On
UniqueConstraintViolationExceptionorDeadlockException: rolls back and retries (up to 3 attempts)
Each retry gets a fresh transaction -- locks are released between attempts to prevent cascading lock waits.
Requirements
The lookup $attributes columns must have a UNIQUE constraint at the database level. Without it, the retry mechanism cannot catch race conditions and duplicate records may be created.
Schema::table('subscriptions', function (Blueprint $table) { $table->unique('external_id'); });
Soft-Delete Awareness
If the target model uses Laravel's SoftDeletes trait, AtomicUpsert automatically:
- Queries
withTrashed()to find soft-deleted records - Restores and updates in a single query (no intermediate visible state)
- Sets
wasRecentlyCreated = falseon the returned model
No configuration needed -- soft-delete detection is automatic via reflection (cached per model class).
Configuration
Custom Primary Key Column
Override getModelKeyColumn() if your models use a non-standard primary key:
use JeromeJHipolito\EloquentAtomic\Traits\AtomicUpsert; class MyService { use AtomicUpsert; protected function getModelKeyColumn(): string { return 'uuid'; } }
DetectsSoftDeletes Trait
Use DetectsSoftDeletes standalone for soft-delete-aware logic in your own code:
use JeromeJHipolito\EloquentAtomic\Traits\DetectsSoftDeletes; class MyService { use DetectsSoftDeletes; public function findRecord(string $modelClass, int $id) { $query = $modelClass::query(); if (static::modelUsesSoftDeletes($modelClass)) { $query->withTrashed(); } return $query->find($id); } }
API
atomicUpdateOrCreate(string $modelClass, array $attributes, array $values): Model
| Parameter | Type | Description |
|---|---|---|
$modelClass |
class-string<T> |
Eloquent model class |
$attributes |
array<string, mixed> |
Lookup columns (must match a unique index) |
$values |
array<string, mixed> |
Columns to create/update |
| Returns | T |
The created or updated model instance |
modelUsesSoftDeletes(string $modelClass): bool
Returns true if the model class uses Laravel's SoftDeletes trait. Results are cached per class.
getModelKeyColumn(): string
Returns the primary key column name. Defaults to 'id'. Override for UUID or custom primary keys.
Database Compatibility
| Feature | MySQL (InnoDB) | PostgreSQL | SQLite |
|---|---|---|---|
lockForUpdate() |
Full support + gap locks | Full support | Ignored (serialized writes) |
| Deadlock recovery | Supported | Supported | N/A |
| Unique constraint retry | Supported | Supported | Supported |
License
MIT
jeromejhipolito/laravel-eloquent-atomic 适用场景与选型建议
jeromejhipolito/laravel-eloquent-atomic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「eloquent」 「concurrency」 「atomic」 「soft-delete」 「upsert」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeromejhipolito/laravel-eloquent-atomic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeromejhipolito/laravel-eloquent-atomic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeromejhipolito/laravel-eloquent-atomic 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tool for managing fast both asynchronous and multi-threaded execution of tasks. Focused on performance and pleasant CLI interface.
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Pleasantly work with asynchronous code.
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-10