Skip to content

dansmaculotte/laravel-enum

 
 

Repository files navigation

Laravel support for spatie/enum

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.

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,
    ];
}

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

Scopes

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');

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 [email protected] 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.

Packages

No packages published

Languages

  • PHP 100.0%