Package Data | |
---|---|
Maintainer Username: | spatie |
Maintainer Contact: | freek@spatie.be (Freek Van der Herten) |
Package Create Date: | 2022-10-20 |
Package Last Update: | 2024-07-08 |
Home Page: | https://spatie.be/open-source |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:08:31 |
Package Statistics | |
---|---|
Total Downloads: | 539,575 |
Monthly Downloads: | 32,542 |
Daily Downloads: | 1,322 |
Total Stars: | 411 |
Total Watchers: | 6 |
Total Forks: | 18 |
Total Open Issues: | 0 |
This package offers a trait that allows you to add flags to an Eloquent model. These can be used to quickly save the state of a process, update, migration, etc... to a model, without having to add an additional column using migrations.
$user->hasFlag('receivedMail'); // returns false
$user->flag('receivedMail'); // flag the user as having received the mail
$user->hasFlag('receivedMail'); // returns true
It also provides scopes to quickly query all models with a certain flag.
User::flagged('myFlag')->get(); // returns all models with the given flag
User::notFlagged('myFlag')->get(); // returns all models without the given flag
Though there are other usages, the primary use case of this package is to easily build idempotent (aka restartable) pieces of code. For example, when writing an Artisan command that sends a mail to each user. Using flags, you can make sure that when the command is cancelled (or fails) half-way through, in the second invocation, a mail will only be sent to users that haven't received one yet.
// in an Artisan command
User::notFlagged('wasSentPromotionMail')
->each(function(User $user) {
Mail::to($user->email)->send(new PromotionMail())
$user->flag('wasSentPromotionMail');
});
});
No matter how many times you would execute this command, users would only get the mail once.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via Composer:
composer require spatie/laravel-model-flags
Behind the scenes, the flags and the relation to a model will be stored in the flags
table.
To create that flags
table, you must publish and run the migrations once with:
php artisan vendor:publish --tag="model-flags-migrations"
php artisan migrate
Optionally, you can publish the config file with:
php artisan vendor:publish --tag="model-flags-config"
This is the contents of the published config file:
return [
/*
* The model used as the flag model.
*/
'flag_model' => Spatie\ModelFlags\Models\Flag::class,
];
To add flaggable behaviour to a model, simply make it use the Spatie\ModelFlags\Models\Concerns\HasFlags
trait
use Illuminate\Database\Eloquent\Model;
use Spatie\ModelFlags\Models\Concerns\HasFlags;
class YourModel extends Model
{
use HasFlags;
}
These functions will become available.
// add a flag
$model->flag('myFlag');
// returns true if the model has a flag with the given name
$model->hasFlag('myFlag');
// remove a flag
$model->unflag('myFlag');
// returns an array with the name of all flags on the model
$model->flagNames();
// use the `flags` relation to delete all flags on a model
$user->flags()->delete();
// use the `flags` relation to delete a particular flag on a model
$user->flags()->where('name', 'myFlag')->delete();
You'll also get these scopes:
// query all models that have a flag with the given name
YourModel::flagged('myFlag');
// query all models that have do not have a flag with the given name
YourModel::notFlagged('myFlag');
To remove a flag from all models in one go, you can delete the flag using the Spatie\ModelFlags\Models\Flag
model.
use Spatie\ModelFlags\Models\Flag;
// remove myFlag from all models
Flag::where('name', 'myFlag')->delete();
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
And a special thanks to Caneco for the logo ✨
The MIT License (MIT). Please see License File for more information.