x-laravel/eloquent-approval
Composer 安装命令:
composer require x-laravel/eloquent-approval
包简介
Approval process for Laravel Eloquent models.
README 文档
README
A Laravel package that adds a three-state approval workflow to Eloquent models — pending, approved, and rejected.
How It Works
- Newly created models are automatically set to pending
- Only approved models are returned by default queries
- Updating attributes that require approval re-suspends the model back to pending
- Status changes dispatch model events you can hook into
Requirements
- PHP ^8.2
- Laravel ^12.0 | ^13.0
Installation
composer require x-laravel/eloquent-approval
The service provider is registered automatically via Laravel's package discovery.
Setup
1. Migration
Add the approval columns to your table:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->approvals(); // adds approval_status (enum) + approval_at (timestamp) $table->timestamps(); });
2. Model
Add the Approvable trait to your model:
use Illuminate\Database\Eloquent\Model; use XLaravel\EloquentApproval\Approvable; class Post extends Model { use Approvable; }
Custom Column Names
class Post extends Model { use Approvable; const APPROVAL_STATUS = 'status'; const APPROVAL_AT = 'status_changed_at'; }
Cast
approval_attodatetimein your model's$caststo getCarboninstances.
Usage
Querying
// Default: only approved records Post::all(); Post::find(1); // null if pending or rejected // Include all statuses Post::withAnyApproval()->get(); Post::withAnyApproval()->find(1); // Filter by status Post::onlyPending()->get(); Post::onlyApproved()->get(); Post::onlyRejected()->get();
To disable the approval scope globally on a model:
class Post extends Model { use Approvable; public $approvalScopeDisabled = true; }
Updating Status
On a model instance
$post->approve(); // ?bool — true on success, false if already approved, null if not persisted $post->reject(); $post->suspend();
On a query builder
Post::whereIn('id', $ids)->approve(); // returns number of updated rows Post::whereIn('id', $ids)->reject(); Post::whereIn('id', $ids)->suspend();
Checking Status
$post->isApproved(); // ?bool $post->isRejected(); // ?bool $post->isPending(); // ?bool
Approval Required Attributes
By default, all attribute changes trigger re-suspension. You can customise this:
class Post extends Model { use Approvable; // Only these attributes trigger re-suspension public function approvalRequired(): array { return ['title', 'body']; } // These attributes never trigger re-suspension public function approvalNotRequired(): array { return ['view_count']; } }
approvalRequired() acts as a blacklist, approvalNotRequired() as a whitelist — same logic as Eloquent's $fillable and $guarded.
Re-suspension only happens when updating via a model instance, not through a query builder.
Duplicate Approvals
Setting the status to its current value is a no-op — no events are dispatched, approval_at is not updated, and the method returns false.
Events
Each approval action dispatches a before and after event:
| Action | Before | After |
|---|---|---|
approve |
approving |
approved |
suspend |
suspending |
suspended |
reject |
rejecting |
rejected |
A general approvalChanged event is also dispatched on every status change.
Returning false from a before-event listener halts the operation.
// Via static callbacks Post::approving(function (Post $post) { // return false to halt }); Post::approved(function (Post $post) { // notify the author }); Post::approvalChanged(function (Post $post) { // fires on any status change }); // Via an observer Post::observe(PostApprovalObserver::class);
class PostApprovalObserver { public function approving(Post $post): void { // } public function approved(Post $post): void { // } }
Factory States
Add ApprovalFactoryStates to your factory to create models with a specific status:
use Illuminate\Database\Eloquent\Factories\Factory; use XLaravel\EloquentApproval\ApprovalFactoryStates; class PostFactory extends Factory { use ApprovalFactoryStates; public function definition(): array { return [ 'title' => fake()->sentence(), ]; } }
Post::factory()->approved()->create(); Post::factory()->rejected()->create(); Post::factory()->suspended()->create();
HTTP Approval Controller
Use the HandlesApproval trait in a controller to handle approval requests:
use App\Http\Controllers\Controller; use App\Models\Post; use XLaravel\EloquentApproval\HandlesApproval; class PostApprovalController extends Controller { use HandlesApproval; protected function model(): string { return Post::class; } }
Route::post('admin/posts/{key}/approval', [PostApprovalController::class, 'performApproval']) ->middleware(['auth', 'can:manage-approvals']);
The request must include an approval_status field with one of: approved, pending, rejected.
Testing
# Build first (once per PHP version) DOCKER_BUILDKIT=0 docker compose --profile php82 build # Run tests docker compose --profile php82 up docker compose --profile php83 up docker compose --profile php84 up docker compose --profile php85 up
License
This package is open-sourced software licensed under the MIT license.
x-laravel/eloquent-approval 适用场景与选型建议
x-laravel/eloquent-approval 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「eloquent」 「approval」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 x-laravel/eloquent-approval 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 x-laravel/eloquent-approval 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 x-laravel/eloquent-approval 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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
CakePHP plugin for approval workflow - users propose changes, admins approve or reject before publishing
Approval Workflow Engine for Filament
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-10