Skip to content

Publishable model

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

Setup a publishable model

With boolean flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasPublishedFlag;
}

Model must have boolean is_published column in database table.

With timestamp flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasPublishedAt;
}

Model must have nullable timestamp published_at column in database table.

Available functions

Get published + not published models

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

Get only published models

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

Get only not published models

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

Publish model

Post::where('id', 4)->publish();

Undo model publish

Post::where('id', 4)->undoPublish();