Skip to content

Approvable model

Anton Komarev edited this page Mar 24, 2020 · 3 revisions

Setup an approvable model

With boolean flag

<?php

namespace App\Models;

use Cog\Flag\Traits\Classic\HasApprovedFlag;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasApprovedFlag;
}

Model must have boolean is_approved column in database table.

With timestamp flag

<?php

namespace App\Models;

use Cog\Flag\Traits\Classic\HasApprovedAt;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasApprovedAt;
}

Model must have nullable timestamp approved_at column in database table.

Available functions

Get approved + not approved models

Post::all();
Post::withNotApproved()->get();

Get only approved models

Post::withoutNotApproved()->get();

Get only not approved models

Post::onlyNotApproved()->get();

Approve model

Post::whereKey(4)->approve();

Undo model approve

Post::whereKey(4)->undoApprove();