A Package to Require Approval Before Peristing Model Data

Packages

July 13th, 2022

A Package to Require Approval Before Peristing Model Data

The Laravel Approval package requires approval of new Model data before being persisted. This package uses Enums and thus requires PHP 8.1 and Laravel 9.

To get started with this package, use the provided MustBeApproved trait on a model:

1use Cjmellor\Approval\Concerns\MustBeApproved;
2 
3class Post extends Model
4{
5 use MustBeApproved;
6 
7 // ...
8}

The package uses a polymorphic relationship to store the data that must be approved in a new table called approvals. You can query approvals and set the state for a given approval using the following methods:

1use App\Models\Approval;
2 
3// Get approvals, rejected models, and pending.
4Approval::approved()->get();
5Approval::rejected()->get();
6Approval::pending()->count();
7 
8// Approve, reject or postpone an approval.
9Approval::where('id', 1)->approve();
10Approval::where('id', 2)->reject();
11Approval::where('id', 3)->postpone();

Finally, if you want to bypass the approval process and persist a model you can use the following method on your model:

1$model->withoutApproval()->update(['title' => 'Some Title']);

You can get started with this package by checking out the repository on Github at cjmellor/approval.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.