Skip to content

Closable model

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

Setup a closable model

With boolean flag

<?php

namespace App\Models;

use Cog\Flag\Traits\Inverse\HasClosedFlag;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasClosedFlag;
}

Model must have boolean is_closed column in database table.

With timestamp flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasClosedAt;
}

Model must have nullable timestamp closed_at column in database table.

Available functions

Get closed + not closed models

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

Get only not closed models

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

Get only closed models

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

Set close flag to model

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

Remove close flag from model

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