A Package to Require Approval Before Peristing Model Data
Published on by Paul Redmond
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.Approval::approved()->get();Approval::rejected()->get();Approval::pending()->count(); // 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.