承接 jeromejhipolito/laravel-eloquent-atomic 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

  1. Starts a database transaction
  2. Attempts SELECT ... FOR UPDATE to find an existing record (including soft-deleted)
  3. If found: restores (if soft-deleted) and updates in a single query
  4. If not found: creates the record
  5. On UniqueConstraintViolationException or DeadlockException: 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 = false on 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 jeromejhipolito/laravel-eloquent-atomic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-10