Skip to content
Laravel support for spatie/enum
Branch: master
Clone or download
Latest commit 7cc694f May 17, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
src use dedicated variable instead of complex condition Apr 26, 2019
tests allow to cast attribute to integer and use enum index Apr 25, 2019
.coveralls.yml add coveralls May 17, 2019
.editorconfig Initial commit Apr 10, 2019
.gitattributes Initial commit Apr 10, 2019
.gitignore Initial commit Apr 10, 2019
.scrutinizer.yml add scrutinizer coverage May 17, 2019
.styleci.yml Initial commit Apr 10, 2019
.travis.yml fix ocular coverage file May 17, 2019 Initial commit Apr 10, 2019 Initial commit Apr 10, 2019 add coveralls badge May 17, 2019
composer.json cleanup composer.json May 17, 2019
phpunit.xml.dist add coveralls May 17, 2019

Laravel support for spatie/enum

Latest Version on Packagist Build Status Quality Score Code Coverage 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,

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


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.

You can’t perform that action at this time.