Package Data | |
---|---|
Maintainer Username: | spatie |
Maintainer Contact: | brent@spatie.be (Brent Roose) |
Package Create Date: | 2019-04-10 |
Package Last Update: | 2024-06-12 |
Home Page: | https://spatie.be/open-source |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:19:56 |
Package Statistics | |
---|---|
Total Downloads: | 3,568,159 |
Monthly Downloads: | 107,786 |
Daily Downloads: | 3,548 |
Total Stars: | 344 |
Total Watchers: | 5 |
Total Forks: | 37 |
Total Open Issues: | 4 |
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->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.
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 freek@spatie.be 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.
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.