bpatyi / laravel-eloquent-flag

Laravel Eloquent flagged attributes behavior.

Home Page:http://cybercog.ru

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Eloquent Flag

Build Status StyleCI Releases License

Eloquent flagged attributes behavior. Add commonly used flags to models very quick and easy.

cog-laravel-eloquent-flag

Features

  • Designed to work with Laravel Eloquent models
  • Each model can has as many flags as required
  • Each flag adds global query scopes to models
  • Covered with unit tests

Available flags list

Trait name Database columns Flag type Logic
HasAcceptedFlag is_accepted Boolean Classic
HasActiveFlag is_active Boolean Classic
HasApprovedFlag is_approved Boolean Classic
HasExpiredInverseFlag is_expired Boolean Inverse
HasKeptFlag is_kept Boolean Classic
HasPublishedFlag is_published Boolean Classic
HasVerifiedFlag is_verified Boolean Classic

How it works

Eloquent Flag is an easy way to add flagged attributes to eloquent models. All flags has their own trait which adds global scopes to desired entity.

All flags separated on 2 logical groups:

  • Classic flags displays only entities with flag setted as true.
  • Inverse flags displays only entities with flag setted as false.

Omitted entities could be retrieved by using special global scope methods, unique for each flag.

Installation

First, pull in the package through Composer.

composer require cybercog/laravel-eloquent-flag

And then include the service provider within app/config/app.php.

// Service provider not using yet. Will be used to boot console commands in future.

'providers' => [
    Cog\Flag\Providers\FlagServiceProvider::class,
];

Usage

Setup an activatable model

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasActiveFlag;
}

Model must have boolean is_active column in database table.

Available functions

Get only active models

Post::all();
Post::withoutInactive();

Get only inactive models

Post::onlyInactive();

Get active + inactive models

Post::withInactive();

Activate model

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

Deactivate model

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

Setup an acceptable model

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasAcceptedFlag;
}

Model must have boolean is_accepted column in database table.

Available functions

Get only accepted models

Post::all();
Post::withoutUnaccepted();

Get only unaccepted models

Post::onlyUnaccepted();

Get accepted + unaccepted models

Post::withUnaccepted();

Accept model

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

Deactivate model

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

Setup an approvable model

<?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.

Available functions

Get only approved models

Post::all();
Post::withoutUnapproved();

Get only unapproved models

Post::onlyUnapproved();

Get approved + unapproved models

Post::withUnapproved();

Approve model

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

Unapprove model

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

Setup a publishable model

<?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.

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

<?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.

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

<?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.

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();

Testing

Run the tests with:

vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email support@cybercog.su instead of using the issue tracker.

Credits

Alternatives

Not found.

Feel free to add more alternatives as Pull Request.

License

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.

cybercog-logo

About

Laravel Eloquent flagged attributes behavior.

http://cybercog.ru

License:MIT License


Languages

Language:PHP 100.0%