Blog Detail


Easily Add Flags to Your Eloquent Models in Laravel cover image

arrow_back Easily Add Flags to Your Eloquent Models in Laravel

Laravel Model Flags is an awesome package that 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


You can install the package via Composer:

composer require spatie/laravel-model-flags

Behind the scenes, the flags and their 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"


To add flaggable behavior 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

// returns true if the model has a flag with the given name

// remove a flag

 // returns an array with the name of all flags on the model

// use the `flags` relation to delete all flags on a model

// 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

// query all models that have do not have a flag with the given name
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();

For more details, please visit Github.

Closing Note

Codebrisk is here to help you grow your business. Our expert developers have plenty of experience in Laravel Custom Web Application Development. We can enhance the performance of applications with easy syntax, third-party API integrations, and built-in functions, followed by a high level of security to prevent unauthorized access to data. We always provide a customized website solution tailored to our client’s needs. So if you’re interested, you can get in touch with us or get a free quote from us.

Published at : 26-10-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project