Skip to content
Laravel support for spatie/enum
Branch: master
Clone or download
Latest commit 7cc694f May 17, 2019
Permalink
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
CHANGELOG.md
CONTRIBUTING.md Initial commit Apr 10, 2019
LICENSE.md Initial commit Apr 10, 2019
README.md add coveralls badge May 17, 2019
composer.json cleanup composer.json May 17, 2019
phpunit.xml.dist add coveralls May 17, 2019

README.md

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.

Installation

You can install the package via composer:

composer require spatie/laravel-enum

Usage

Casting

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:

```php
$model->status = 'published';

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

$model->fill($request->validated());

//

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

//

$model->status->isEqual(StatusEnum::ARCHIVED());

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.

Roadmap

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

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security

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

Postcardware

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.

Credits

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.

License

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

You can’t perform that action at this time.