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 |
---|---|---|
HasAcceptedFlag |
is_accepted |
Boolean |
HasActiveFlag |
is_active |
Boolean |
HasApprovedFlag |
is_approved |
Boolean |
HasKeptFlag |
is_kept |
Boolean |
HasPublishedFlag |
is_published |
Boolean |
HasVerifiedFlag |
is_verified |
Boolean |
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.
The main logic of the flags: If flag is false
- entity should be hidden from the query results. Omitted entities could be retrieved by using special global scope methods.
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\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\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\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\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\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\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.
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.