-
-
Notifications
You must be signed in to change notification settings - Fork 20
Usage
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.
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.
<?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.
<?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.
Post::all();
Post::withoutDisapproved();
Post::onlyDisapproved();
Post::withDisapproved();
Post::where('id', 4)->approve();
Post::where('id', 4)->disapprove();
<?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.
<?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.
Post::all();
Post::withoutUnpublished();
Post::onlyUnpublished();
Post::withUnpublished();
Post::where('id', 4)->publish();
Post::where('id', 4)->unpublish();
<?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.
<?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.
Post::all();
Post::withoutUnverified();
Post::onlyUnverified();
Post::withUnverified();
Post::where('id', 4)->verify();
Post::where('id', 4)->unverify();
Keep functionality required when you are trying to attach related models before parent one isn't persisted in application.
Issue:
- User press
Create Post
button. - Create post form has image uploader.
- On image uploading user can't attach image to post before post entity wouldn't been stored in database.
Solution:
- Add
HasKeptFlag
trait to model (and add booleanis_kept
column to model's database table). - Create empty model on form loading (it will has
is_kept = 0
by default). - Feel free to add any relations to the model.
- 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.
Post::all();
Post::withoutUnkept();
Post::onlyUnkept();
Post::withUnkept();
Post::where('id', 4)->keep();
Post::where('id', 4)->unkeep();
Post::onlyUnkeptOlderThanHours(4);
Output will have all unkept models created earlier than 4 hours ago.
<?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.
<?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.
Post::all();
Post::withoutExpired();
Post::onlyExpired();
Post::withExpired();
Post::where('id', 4)->expire();
Post::where('id', 4)->unexpire();
<?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.
<?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.
Post::all();
Post::withoutClosed();
Post::onlyClosed();
Post::withClosed();
Post::where('id', 4)->close();
Post::where('id', 4)->open();