• Reading time ~ 1 min
  • 10.09.2022

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.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297