ysm/with-transaction
Composer 安装命令:
composer require ysm/with-transaction
包简介
Laravel trait to automatically wrap model operations in transactions.
关键字:
README 文档
README
This package provides a reusable Laravel trait that automatically wraps Eloquent model
operations (create, update, save, delete, etc.) in database transactions — giving you cleaner, safer code
without writing DB::transaction() everywhere.
✨ Features
- Automatically wraps model actions in a transaction
- Easily toggle transaction behavior per operation
- Works with all Eloquent models
- Supports create, update, save, delete, restore, forceDelete
- Static helpers:
withTransaction,transaction
📦 Installation
composer require ysm/with-transaction php artisan vendor:publish --tag=with-transaction-config
📖 Usage
1. Add the Trait
In your Eloquent model, use the WithTransaction trait:
use App\Traits\WithTransaction; class Post extends Model { use WithTransaction; protected $fillable = ['title', 'content']; }
2. Use in the controller
Now you can use your model as usual, and all operations will be automatically wrapped in a transaction:
namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class PostController extends Controller { public function store(Request $request) { $post = Post::transaction(function () use ($request) { $model = Post::create($request->validated()); $model->tags()->attach(Tag::inRandomOrder()->take(3)->pluck('id')); $model->categories()->attach(Category::inRandomOrder()->take(3)->pluck('id')); $model->addMediaFromRequest('image') ->toMediaCollection('posts'); // all or nothing rollback automatically return $model; }); return response()->json([ 'message' => 'Post created successfully', 'post' => $post, ]); } }
3. Updating the Model
You can also use the same trait for updating models:
namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\Post; use Illuminate\Http\Request; class PostController extends Controller { public function Update(Request $request, Post $post) { $post = Post::transaction(function () use ($post) { $post->update([ 'title' => 'Update title Post v1', ]); $post->tags()->sync([4, 5, 6]); return $post; }); return response()->json([ 'message' => 'Post updated successfully', 'post' => $post, ], 200); } }
4. Deleting the Model
You can also delete models using the same trait:
namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\Post; use Illuminate\Http\Request; class PostController extends Controller { public function destroy(Post $post) { $post = Post::transaction(function () use ($post) { $post->delete(); // If you want to perform any additional operations after deletion, // you can do so here. For example, logging or cleaning up related data. // If the deletion fails, the transaction will automatically roll back. return $post; }); return response()->json([ 'message' => 'Post deleted successfully', 'post' => $post, ], 200); } }
Or you can use it as a helper function
namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\Post; use Illuminate\Http\Request; use YSM\Support\transaction; class PostController extends Controller { public function destroy(Post $post) { $post = transaction(function () use ($post) { $post->delete(); // If you want to perform any additional operations after deletion, // you can do so here. For example, logging or cleaning up related data. // If the deletion fails, the transaction will automatically roll back. return $post; }); return response()->json([ 'message' => 'Post deleted successfully', 'post' => $post, ], 200); } }
🔁 Using the Global Helper Function
Use anywhere in your app:
use function YSM\Support\transaction; transaction(function () use ($post) { $post->update([...]); }, attempts: 2, onSuccess: fn () => Log::info('Done!'), onFailure: fn ($e) => report($e));
🧠 Fluent Transaction Builder
Need more control? Use the fluent interface:
use YSM\Support\Facades\Transaction; Transaction::start() ->attempts(3) ->onSuccess(fn ($result) => Log::info('Transaction success', ['id' => $result?->id])) ->onFailure(fn ($e) => Log::error('Transaction failed', ['message' => $e->getMessage()])) ->run(fn () => Post::create([...]));
❤️ Thank You
Thanks for using this package! If it saved you time or avoided a bug, consider giving it a ⭐ on GitHub.
ysm/with-transaction 适用场景与选型建议
ysm/with-transaction 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 10, 最近一次更新时间为 2025 年 06 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「model」 「trait」 「laravel」 「transaction」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ysm/with-transaction 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ysm/with-transaction 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ysm/with-transaction 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Trait providing methods to set class properties with an array.
Expansion traits for Yii2 components
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-28