Skip to content

Activatable model

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

Setup an activatable model

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasActiveFlag;
}

Model must have boolean is_active column in database table.

Available functions

Get activated + not activated models

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

Get only active models

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

Get only not activated models

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

Activate model

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

Undo model activation

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