Blog Detail

13

Jul
Approve New Model Data Before it is persisted in Laravel cover image

arrow_back Approve New Model Data Before it is persisted in Laravel

Approval is a fantastic Laravel package that provides a simple way to approve new Model data before it is persisted.

Installation

You can install the package via composer:

composer require cjmellor/approval

You can publish and run the migrations with:

php artisan vendor:publish --tag="approval-migrations"

php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="approval-config"

The config allows you to change the polymorphic pivot name. It should end with able though.

Usage

Add the MustBeApproved trait to your Model and now the data will be stored in an approvals table, ready for you to approve or deny.

For example, you add it to a Post Model and each time a Post is created or updated, all the dirty data will be stored in the database as JSON for you to do something with it.

<?php

use Cjmellor\Approval\Concerns\MustBeApproved;

class Post extends Model
{
    use MustBeApproved;

    // ...
}

All Models using the Trait will now be stored in a new table – approvals. This is a polymorphic relationship.

Here is some info about the columns in the approvals table:

approvalable_type => The class name of the Model that the approval is for

approvalable_id => The ID of the Model that the approval is for

state => The state of the approval. This uses an Enum class. This column is cast to an ApprovalStatus Enum class

new_data => All the fields created or updated in the Model. This is a JSON column. This column is cast to the AsArrayObject Cast

original_data => All the fields in the Model before they were updated. This is a JSON column. This column is cast to the AsArrayObject Cast

If you want to check if the Model data will be bypassed, use the isApprovalBypassed method.

return $model->isApprovalBypassed();

Scopes

The package comes with some helper methods for the Builder, utilizing a custom scope - ApprovalStateScope

By default, all queries to the approvals table will return all the Models no matter the state.

There are three methods to help you retrieve the state of Approval.

<?php

use App\Models\Approval;

Approval::approved()->get();
Approval::rejected()->get();
Approval::pending()->count();

You can also set a state for approval:

<?php

use App\Models\Approval;

Approval::where('id', 1)->approve();
Approval::where('id', 2)->reject();
Approval::where('id', 3)->postpone();

If you need to reset a state, you can use the withAnyState helper.

Please visit Github for more details.

Published at : 13-07-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

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 your project

Launch project