Skip to content

Expirable model

Anton Komarev edited this page Mar 19, 2019 · 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 only not expired models

Post::withoutExpired();

Get only expired models

Post::onlyExpired();

Set expire flag to model

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

Remove expire flag from model

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