traknpay/laravel-eloquent-approval

Approval Package For Laravel Eloquent Model

1.4 2024-02-01 12:14 UTC

This package is not auto-updated.

Last update: 2024-05-23 13:49:35 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

Laravel Version Package Version
5.6.* ^1.0

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.

  1. sendingForApproval - before saving data into 'approval' table.
  2. 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.