Eloquent flagged attributes behavior. Add commonly used flags to models very quick and easy.
- 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
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 |
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 astrue
.Inverse
flags displays only entities with flag setted asfalse
.
Omitted entities could be retrieved by using special global scope methods, unique for each flag.
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,
];
<?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.
Post::all();
Post::withoutInactive();
Post::onlyInactive();
Post::withInactive();
Post::where('id', 4)->activate();
Post::where('id', 4)->deactivate();
<?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.
Post::all();
Post::withoutUnaccepted();
Post::onlyUnaccepted();
Post::withUnaccepted();
Post::where('id', 4)->accept();
Post::where('id', 4)->unaccept();
<?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.
Post::all();
Post::withoutUnapproved();
Post::onlyUnapproved();
Post::withUnapproved();
Post::where('id', 4)->approve();
Post::where('id', 4)->unapprove();
<?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.
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.
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.
Post::all();
Post::withoutExpired();
Post::onlyExpired();
Post::withExpired();
Post::where('id', 4)->expire();
Post::where('id', 4)->unexpire();
Run the tests with:
vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email support@cybercog.su instead of using the issue tracker.
Not found.
Feel free to add more alternatives as Pull Request.
Laravel Eloquent Flag
package is open-sourced software licensed under the MIT license.Check Mark
image licensed under Creative Commons 3.0 by Kimmi Studio.Clock Check
image licensed under Creative Commons 3.0 by Harsha Rai.
CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.