A Package to Require Approval Before Peristing Model Data


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:

use Cjmellor\Approval\Concerns\MustBeApproved;
class Post extends Model
use MustBeApproved;
// ...

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:

use App\Models\Approval;
// Get approvals, rejected models, and pending.
// Approve, reject or postpone an approval.
Approval::where('id', 1)->approve();
Approval::where('id', 2)->reject();
Approval::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:

$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.