Skip to content

Expirable model

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

Setup an expirable model

With boolean flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasExpiredFlag;
}

Model must have boolean is_expired column in database table.

With timestamp flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasExpiredAt;
}

Model must have nullable timestamp expired_at column in database table.

Available functions

Get expired + not expired models

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

Get only not expired models

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

Get only expired models

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

Set expire flag to model

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

Remove expire flag from model

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