Skip to content
Pe Ell edited this page Apr 17, 2017 · 11 revisions

Prepare database

Boolean flag

public function up()
{
    Schema::create('post', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->boolean('is_published');
        $table->timestamps();
    });
}

Change is_published on any other Boolean flag database column name.

Timestamp flag

public function up()
{
    Schema::create('post', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->timestamp('published_at')->nullable();
        $table->timestamps();
    });
}

Change published_at on any other Timestamp flag database column name.

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 only not closed models

Post::all();
Post::withoutClosed();

Get only closed models

Post::onlyClosed();

Get closed + not closed models

Post::withClosed();

Set close flag to model

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

Remove close flag from model

Post::where('id', 4)->open();
Clone this wiki locally