Laravel enum support

1.2.0 2020-01-16 15:26 UTC

This package is auto-updated.

Last update: 2020-01-16 15:26:50 UTC


Latest Version on Packagist GitHub Workflow Status Quality Score Code Coverage StyleCI Total Downloads

This package provides extended support for our spatie/enum package in Laravel.


You can install the package via composer:

composer require spatie/laravel-enum



Chances are that if you're working in a Laravel project, you'll want to use enums within your models. This package provides a trait you can use in these models, to allow allow automatic casting between stored enum values and enum objects.

use Spatie\Enum\HasEnums;

class TestModel extends Model
    use HasEnums;

    protected $enums = [
        'status' => TestModelStatus::class,

You can also define enum as nullable:

protected $enums = [
    'status' => TestModelStatus::class.':nullable',

By using the HasEnums trait, you'll be able to work with the status field like so:

$model = TestModel::create([
    'status' => StatusEnum::DRAFT(),

You can set the value of an enum field with its textual value:

$model->status = 'published';

This can be useful when filling data from a validated request:



$model->status = StatusEnum::PUBLISHED();



In some cases, enums should be stored as integer (index) in the database.

By using the $casts property you can cast your status attribute to int or integer and the trait will store the index instead of the value.


  • Models fields
  • Model scopes #4
  • Validation #5
  • Request Transformation #7


The HasEnums trait also provides some useful scopes to query your database. These scopes will also take the optional mapping you provided into account.

Post::whereEnum('status', StatusEnum::DRAFT());

Post::whereNotEnum('status', StatusEnum::PUBLISHED());

You may provide multiple enums as an array:

Post::whereEnum('status', [StatusEnum::DRAFT(), StatusEnum::ARCHIVED()]);

Post::whereNotEnum('status', [StatusEnum::PUBLISHED()]);

You may also provide textual input:

Post::whereEnum('status', 'archived');
Post::whereEnum('status', 'legacy archived value');


composer test


Please see CHANGELOG for more information on what has changed recently.


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.


You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.


Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.


The MIT License (MIT). Please see License File for more information.