traknpay / laravel-eloquent-approval
Approval Package For Laravel Eloquent Model
Requires
- php: ^7.0|^8.0
- illuminate/database: ^5.6.0|^6.0
- illuminate/support: ^5.6.0|^6.0
This package is not auto-updated.
Last update: 2024-11-07 16:01:26 UTC
README
Approval process for Laravel's Eloquent models.
How it works?
New entities are created in the 'approval' table as pending and then can become approved or rejected.
Install
$ composer require traknpay/laravel-eloquent-approval
Version Compatibility
Setup
Registering the service provider
By default the service provider is registered automatically by Laravel package
discovery otherwise you need to register it in your config\app.php
TraknPay\EloquentApproval\ApprovalServiceProvider::class
Run the following commands to migrate the 'approval' table:
php artisan vendor:publish --provider='TraknPay\EloquentApproval\ApprovalServiceProvider' php artisan migrate
Model
Add ApprovalTrait
trait to the model and override the isApprover()
function as per your need.
use Illuminate\Database\Eloquent\Model; use TraknPay\EloquentApproval\ApprovalTrait; class Entity extends Model { use ApprovalTrait; public static function isApprover(): bool { return true; } }
By default isApprover()
is true and if you use this trait, it will not put the new entities in the approval
table. You can override this functionality based on your need. If isApprover()
function returns false then entities are added to approval
table and mark the transaction as false.
Hence, if 'isApprover()' returns false the model will not be persited to database instead added to 'approval' table for approval.
For example, in this function you can check whether user has permission to approve or not.
##Approval Events
Following model events are dispatched before and after saving data into 'approval' table.
sendingForApproval
- before saving data into 'approval' table.sentForApproval
- after saving data into 'approval' table.
Inspirations
I got some inspiration from mtvs/eloquent-approval package. Even though , I wrote my own package for my project purpose I got some insights on how to write my own package from this.