mindtwo / laravel-enumerable
Laravel Enumerable
Installs: 31
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:package
Requires
- php: ^7.2
- illuminate/database: 5.6.*|5.7.*|5.8.*|6.*|7.*
- illuminate/support: 5.6.*|5.7.*|5.8.*|6.*|7.*
- mindtwo/laravel-dynamic-model-mutators: ^3.1
- mindtwo/laravel-enum: ^2.1
Requires (Dev)
- illuminate/database: 7.*
- phpunit/phpunit: ^8.5
README
Installation
You can install the package via composer:
composer require mindtwo/laravel-enumerable
How to use?
Setting up enumerable objects
This package based on BenSampo/laravel-enum. Take a look at its Documentation to set up your enumerable objects.
Prepare eloquent model
There are two ways to use this package. The simplest way is to extend the EnumerableModel which is shipped with this package.
namespace example; use mindtwo\LaravelEnumerable\Models\EnumerableModel class exampleModel extends EnumerableModel { }
If you prefer, you can directly use the Enumerable trait in your models. Be sure to implement the EnumerableInterface, too.
namespace example; use Illuminate\Database\Eloquent\Model; use mindtwo\LaravelEnumerable\Interfaces\EnumInterface; use mindtwo\LaravelEnumerable\Models\Traits\Enumerable; class exampleModel extends Model implements EnumInterface { use Enumerable; }
Configure eloquent model
To configure enumerable attributes simply set up a property named 'enums' as array. The key contains the attribute name. The value is set to the enumerable class name you like to use for that attribute.
namespace example; use mindtwo\LaravelEnumerable\Models\EnumerableModel class exampleModel extends EnumerableModel { public $enums = [ 'examle_attribute' => ExampleEnum::class ]; }
Important: Since package version 1.1.0 the $enums property must be declared as public!
Now you can use Laravel's regular attribute set and fill functions to set the attribute value. If the value is not configured in the given enum object, an InvalidEnumValueException is thrown.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email info@mindtwo.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.