### Install Package via Composer Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Instructions for installing the spatie/laravel-model-flags package using Composer, the dependency manager for PHP. ```bash composer require spatie/laravel-model-flags ``` -------------------------------- ### Idempotent Artisan Command Example Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Illustrates how to use flags within an Artisan command to ensure operations are idempotent. This example ensures emails are sent only once to users, even if the command is run multiple times. ```php // 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. ``` -------------------------------- ### Get Flag Names and Last Flagged At Times Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Retrieve all flag names associated with a model using `flagNames()`. You can also get the timestamp of the last time a flag was applied or updated using `lastFlaggedAt()`, optionally specifying a flag name. ```php // returns an array with the name of all flags on the model $model->flagNames(); // returns the update time of the lastly updated flag $model->lastFlaggedAt(); // returns the updated_at of the `myFlag` flag on the model $model->lastFlaggedAt('myFlag'); // returns null if there is no flag with the given name $model->lastFlaggedAt('doesNotExist'); ``` -------------------------------- ### Publish and Run Migrations Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Steps to publish the package's migration files and then run them to create the necessary database table for storing flags. ```bash php artisan vendor:publish --tag="model-flags-migrations" php artisan migrate ``` -------------------------------- ### Publish Configuration File Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Command to publish the package's configuration file, allowing customization of settings like the flag model. ```bash php artisan vendor:publish --tag="model-flags-config" ``` -------------------------------- ### Run Tests Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md To ensure the package is functioning correctly or to contribute, you can run the provided test suite. ```bash composer test ``` -------------------------------- ### Default Configuration Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md The default configuration file content for the Laravel Model Flags package, specifying the model used for flags. ```php return [ /* * The model used as the flag model. */ 'flag_model' => Spatie\ModelFlags\Models\Flag::class, ]; ``` -------------------------------- ### Query Models by Flag Status Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md The package provides convenient Eloquent scopes to query models based on whether they have a specific flag or not. Use `flagged()` to find models with a flag and `notFlagged()` for models without it. ```php // 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'); ``` -------------------------------- ### Model Flagging Operations Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Demonstrates how to use the Model Flags trait to check, set, and unset flags on Eloquent models. This allows for state management without altering the database schema. ```php $user->hasFlag('receivedMail'); // returns false $user->flag('receivedMail'); // flag the user as having received the mail $user->hasFlag('receivedMail'); // returns true ``` -------------------------------- ### Manage Model Flags (Add, Check, Remove) Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Once the trait is applied, you can use the provided methods to interact with flags. This includes adding a flag, checking if a model possesses a specific flag, and removing a flag. ```php // 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'); ``` -------------------------------- ### Handle Duplicate Flagging (Update Timestamp) Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md A flag can only exist once per model. If you attempt to flag a model with a flag that already exists, the `updated_at` attribute of that flag will be updated instead of creating a new entry. ```php $model->flag('myFlag'); // after a while $model->flag('myFlag'); // update_at will be updated ``` -------------------------------- ### Querying Models by Flag Status Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Provides Eloquent scopes to filter models based on whether they possess a specific flag or not. This is useful for querying datasets based on their state. ```php User::flagged('myFlag')->get(); // returns all models with the given flag User::notFlagged('myFlag')->get(); // returns all models without the given flag ``` -------------------------------- ### Use HasFlags Trait in Eloquent Model Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md To enable flag functionality, make your Eloquent model use the `Spatie\ModelFlags\Models\Concerns\HasFlags` trait. This trait provides all the necessary methods and behaviors for managing flags. ```php use Illuminate\Database\Eloquent\Model; use Spatie\ModelFlags\Models\Concerns\HasFlags; class YourModel extends Model { use HasFlags; } ``` -------------------------------- ### Delete Flags via Relation or Model Source: https://github.com/spatie/laravel-model-flags/blob/main/README.md Flags can be removed individually or in bulk. You can use the `flags` relation to delete specific flags or all flags from a model. Alternatively, you can directly delete flags from the `Flag` model to remove them from all associated models. ```php // 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(); // remove myFlag from all models use Spatie\ModelFlags\Models\Flag; Flag::where('name', 'myFlag')->delete(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.