lemax10 / laravel-enums
Simple Enum implementation for Laravel. With ability to casting for Eloquent models and labels translations via lang files.
Installs: 1 808
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=7.1
- illuminate/console: 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^v7.0|^8.0|^9.0|^10.0
- illuminate/database: 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^v7.0|^8.0|^9.0|^10.0
- illuminate/support: 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^v7.0|^8.0|^9.0|^10.0
- illuminate/validation: 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^v7.0|^8.0|^9.0|^10.0
- myclabs/php-enum: ^1.7
Requires (Dev)
- phpunit/phpunit: ~4.8.36 || ^5.7.27 || ^6.5.8
README
THIS PACKAGE IS STILL IN DEVELOPMENT, DO NOT USE YET
Laravel Enums
The package is based on the implementation of Enum from myclabs/php-enum.
ExampleModel
use App\Enums\ExampleStatusEnum; use Illuminate\Database\Eloquent\Model; use LeMaX10\Enums\Traits\ModelEnums; /** * Class ExampleModel * @package App */ class ExampleModel extends Model { use ModelEnums; /** * @var array */ protected $enums = [ 'status' => ExampleStatusEnum::class ]; }
ExampleStatusEnum
use LeMaX10\Enums\Enum; /** * Class ExampleStatusEnum * @package App\Enums */ final class ExampleStatusEnum extends Enum { /** * */ private const FOO = 'foo'; /** * */ private const BAR = 'bar'; }
ExampleTranslatableEnum
use LeMaX10\Enums\Enum; use LeMaX10\Enums\Contracts\Translatable; /** * Class ExampleStatusEnum * @package App\Enums */ final class ExampleStatusEnum extends Enum implements Translatable { /** * */ private const FOO = 'foo'; /** * */ private const BAR = 'bar'; /** * Translatable value * @return string */ public function getTransValue(): string { return trans('enum.'. $this->value); } }
Request Validation Rules
<?php namespace App\Http\Requests; use App\Enums\ExampleStatusEnum; use Illuminate\Http\Request; class ExampleRequest extends Request { public function rules(): array { return [ 'status' => 'required|enum:'. ExampleStatusEnum::class ]; } }
or
<?php namespace App\Http\Requests; use App\Enums\ExampleStatusEnum; use LeMaX10\Enums\Rules\EnumValue; use Illuminate\Http\Request; class ExampleRequest extends Request { public function rules(): array { return [ 'status' => ['required', new EnumValue(ExampleStatusEnum::class)] ]; } }
or
<?php namespace App\Http\Requests; use App\Enums\ExampleStatusEnum; use Illuminate\Http\Request; class ExampleRequest extends Request { public function rules(): array { return [ 'status' => ['required', ExampleStatusEnum::rule()] ]; } }
Installation
You can install the package via composer:
composer require LeMaX10/laravel-enums
Register the service provider in config/app.php from Laravel < 5.5:
'providers' => [ ... \LeMaX10\Enums\EnumServiceProvider::class, ... ]
Migration or Sync values
You can synchronize the enums created in the database using the sync command.
Example
- Create enum column or change exists column in table and run
Schema::create('tableName', function (Blueprint $table): void { $table->increments('id'); $table->enum('status', array_values(ExampleStatusEnum::toArray())); });
- Run sync command php artisan db:enum:sync App\Models\ExampleModel
Usage
$exampleModel = new ExampleModel; $exampleModel->status = ExampleStatusEnum::FOO(); $exampleModel->save(); .... echo $exampleModel->status
Documentation
See documentation myclabs/php-enum.
Testing
You can run the tests with:
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 rdlrobot@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.