16
JunThe Eloquent Approval Package is introduced by Mohamed Ali Tavasoli which provides an Approval process for Laravel’s Eloquent models. After this package’s setup, when new entities are being created, they are marked as pending. Then their status can be changed to approved or rejected. When querying the model only approved entities are included, i.e.: rejected entities, as well as pending ones, are excluded. You can include those by explicitly specifying them. When an update occurs that modifies attributes that require approval the entity becomes suspended again.
You can install this package via composer by running this command:
$ composer require mtvs/eloquent-approval
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
Mtvs\EloquentApproval\ApprovalServiceProvider::class
Database
The following method adds two columns to the schema, one to store the approval status named approval_status
and another to store the timestamp at which the last status update is occurred named approval_at
.
$table->approvals()
You can change the default column names but then you need to specify them on the model too.
Model
Add Approvable
trait to the model
use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentApproval\Approvable;
class Entity extends Model
{
use Approvable;
}
If you want to change the default column names you need to specify them by adding class constants to your model
use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentApproval\Approvable;
class Entity extends Model
{
use Approvable;
const APPROVAL_STATUS = 'custom_approval_status';
const APPROVAL_AT = 'custom_approval_at';
}
Add approval_at
to the model $dates
list to get Carbon
instances when accessing it.
Approval Required Attributes
When an update occurs that modifies attributes that require approval, the entity becomes suspended again.
$entity->update($attributes); // an update with approval required modification
$entity->isPending(); // true
Newly created entities are marked as pending and by default excluded from queries on the model.
Entity::create(); // #1 pending
Entity::all(); // []
Entity::find(1); // null
Including all the entities
Entity::anyApprovalStatus()->get(); // retrieving all
Entity::anyApprovalStatus()->find(1); // retrieving one
Entity::anyApprovalStatus()->delete(); // deleting all
Limiting to only a specific status
Entity::onlyPending()->get(); // retrieving only pending entities
Entity::onlyRejected()->get(); // retrieving only rejected entities
Entity::onlyApproved()->get(); // retrieving only approved entities
Updating the status On model objects
You can update the status of an entity by using provided methods on the Model object.
$entity->approve(); // returns bool if the entity exists otherwise null
$entity->reject(); // returns bool if the entity exists otherwise null
$entity->suspend(); // returns bool if the entity exists otherwise null
Trying to set the approval status to the current value is ignored, i.e.: no event will be dispatched and the approval timestamp won’t be updated. In this case, the approval method returns false
.
This package has a lot more features and options with code examples. If you want to dig more, you can visit its complete documentation on Github.
We are highly results-driven experts having experience in the grounds of Laravel web application development, CRM software, SAAS applications, and eCommerce web apps. So If you want to develop a custom web app you can contact us or Start a project with us.
Published at : 16-06-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project