PHP code example of cjmellor / approval

1. Go to this page and download the library: Download cjmellor/approval library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cjmellor / approval example snippets


return [
    'approval' => [
        /**
         * The approval polymorphic pivot name
         *
         * Default: 'approvalable'
         */
        'approval_pivot' => 'approvalable',
    ],
];



use Cjmellor\Approval\Concerns\MustBeApproved;

class Post extends Model
{
    use MustBeApproved;

    // ...
}

return $model->isApprovalBypassed();

Post::create(['title' => 'Some Title']);

Post::create(['title' => 'Some Title', 'user_id' => 1]);

public function getApprovalForeignKeyName(): string
{
    return 'author_id';
}



use App\Models\Approval;

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



use App\Models\Approval;

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

$approval->approveIf(true);
$approval->rejectIf(false);
$approval->postponeIf(true);

$approval->approveUnless(false);
$approval->rejectUnless(true);
$approval->postponeUnless(false);

// Get the requestor (creator) of the approval
$requestor = $approval->requestor;

// Filter approvals by requestor
$userApprovals = Approval::requestedBy($user)->get();

// Check if an approval was requested by a specific user
if ($approval->wasRequestedBy($user)) {
    // Do something
}

- ModelApproved::class
- ModelPostponed::class
- ModelRejected::class
- ApprovalCreated::class

'states' => [
    'pending' => [
        'name' => 'Pending',
        'default' => true,
    ],
    'approved' => [
        'name' => 'Approved',
    ],
    'rejected' => [
        'name' => 'Rejected',
    ],
    'in_review' => [
        'name' => 'In Review',
    ],
    'needs_info' => [
        'name' => 'Needs Clarification',
    ],
],

// Set a custom state
$approval->setState('in_review');

// Check the current state
$currentState = $approval->getState();

// Query approvals with a specific state
$inReviewApprovals = Approval::whereState('in_review')->get();

// The standard scopes still work for the default states
$pendingApprovals = Approval::pending()->get();

Approval::first()->rollback();

Approval::first()->rollback(bypass: false); // default is true

Approval::first()->rollback(fn () => true);

// ModelRolledBackEvent::class

public Model $approval,
public Authenticatable|null $user,

// Set expiration in hours (most common)
Approval::find(1)->expiresIn(hours: 24);

// Set expiration in minutes
Approval::find(1)->expiresIn(minutes: 30);

// Set expiration in days
Approval::find(1)->expiresIn(days: 7);

// Set specific expiration datetime
Approval::find(1)->expiresIn(datetime: now()->addWeek());

// Automatically reject when expired
Approval::find(1)->expiresIn(hours: 48)->thenReject();

// Automatically postpone (set to pending) when expired
Approval::find(1)->expiresIn(hours: 48)->thenPostpone();

// Use a custom action through event listeners
Approval::find(1)->expiresIn(hours: 48)->thenDo(function($approval) {
    // This callback is for documentation only
    // Implement an event listener for ApprovalExpired event
});

// In App\Console\Kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule->command('approval:process-expired')->everyMinute();
}

// Get all expired approvals
Approval::expired()->get();

// Get all non-expired approvals (including those with no expiration)
Approval::notExpired()->get();

// Get all approvals that have an expiration set
Approval::hasExpiration()->get();

// Check if a specific approval is expired
$approval->isExpired();

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



use Cjmellor\Approval\Concerns\MustBeApproved;

class Post extends Model
{
    use MustBeApproved;

    protected array $approvalAttributes = ['name'];

    // ...
}
bash
php artisan vendor:publish --tag="approval-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="approval-config"