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 an approvable model

With boolean flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasApprovedFlag;
}

Model must have boolean is_approved column in database table.

With timestamp flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasApprovedAt;
}

Model must have nullable timestamp approved_at column in database table.

Available functions

Get only approved models

Post::all();
Post::withoutDisapproved();

Get only disapproved models

Post::onlyDisapproved();

Get approved + disapproved models

Post::withDisapproved();

Approve model

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

Disapprove model

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

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 only published models

Post::all();
Post::withoutUnpublished();

Get only unpublished models

Post::onlyUnpublished();

Get published + unpublished models

Post::withUnpublished();

Publish model

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

Unpublish model

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

Setup a verifiable model

With boolean flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasVerifiedFlag;
}

Model must have boolean is_verified column in database table.

With timestamp flag

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasVerifiedAt;
}

Model must have nullable timestamp verified_at column in database table.

Available functions

Get only verified models

Post::all();
Post::withoutUnverified();

Get only unverified models

Post::onlyUnverified();

Get verified + unverified models

Post::withUnverified();

Verify model

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

Unverify model

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

Setup a keepable model

Keep functionality required when you are trying to attach related models before parent one isn't persisted in application.

Issue:

  1. User press Create Post button.
  2. Create post form has image uploader.
  3. On image uploading user can't attach image to post before post entity wouldn't been stored in database.

Solution:

  1. Add HasKeptFlag trait to model (and add boolean is_kept column to model's database table).
  2. Create empty model on form loading (it will has is_kept = 0 by default).
  3. Feel free to add any relations to the model.
  4. Model will be marked as required to be kept as soon as model will be saved\updated for the first time after creation.

Known limitations:

  • Using this methodology you wouldn't have create form, only edit will be available.
  • Not all the models allows to have empty attributes on save. Such attributes could be set as nullable to allow create blank model.
  • To prevent spam of unkept models in database they could be deleted on a predetermined schedule (once a week for example).
<?php

namespace App\Models;

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

class Post extends Model
{
    use HasKeptFlag;
}

Your model is now can be marked to be kept!

Model must have boolean is_kept column in database table.

By default all records that have a is_kept equals to 0 will be excluded from your query results. To include unkept records, all you need to do is call the withUnkept() method on your query.

Available functions

Get only kept models

Post::all();
Post::withoutUnkept();

Get only unkept models

Post::onlyUnkept();

Get kept + unkept models

Post::withUnkept();

Keep model

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

Unkeep model

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

Get unkept models which older than hours

Post::onlyUnkeptOlderThanHours(4);

Output will have all unkept models created earlier than 4 hours ago.

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

Post::all();
Post::withoutExpired();

Get only expired models

Post::onlyExpired();

Get expired + not expired models

Post::withExpired();

Set expire flag to model

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

Remove expire flag from model

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

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