rickgoemans / laravel-enum-helpers
A Laravel helper to have nice enum related functions
v4.1.0
2025-03-01 06:38 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
README
A Laravel helper to have nice enum related functions
This package contains some useful helpers for Laravel and PHP's enums.
Installation
You can install the package via composer:
composer require rickgoemans/laravel-enum-helpers
Usage
Example enum:
<?php namespace Rickgoemans\LaravelEnumHelpers\Enums; use Rickgoemans\LaravelEnumHelpers\Traits\HasEnumHelpers; enum CreditCheckRating: string { use HasEnumHelpers; case AAA = 'AAA'; case AA = 'AA'; case A = 'A'; case BBB = 'BBB'; case BB = 'BB'; case B = 'B'; case CCC = 'CCC'; case CC = 'CC'; case C = 'C'; case D = 'D'; /** @inheritdoc */ public static function badgeColors(): array { return [ self::AAA->label() => 'success', self::AA->label() => 'success', self::A->label() => 'success', self::BBB->label() => 'info', self::BB->label() => 'info', self::B->label() => 'info', self::CCC->label() => 'info', self::CC->label() => 'info', self::C->label() => 'info', self::D->label() => 'danger', self::NR->label() => 'danger', null => 'danger', ]; } /** @inheritdoc */ public static function order(): array { return [ self::AAA, self::AA, self::A, self::BBB, self::BB, self::B, self::CCC, self::CC, self::C, self::D, self::NR, ]; } /** @inheritdoc */ public function label(): string { return $this->baseLabel('credit_checks.rating.values.'); } }
Example Laravel Nova resource with badge and select fields usage:
<?php namespace App\Nova; use Laravel\Nova\Fields\Badge; use Laravel\Nova\Resource; use Rickgoemans\LaravelEnumHelpers\Enums\CreditCheckRating; class CreditCheck extends Resource { // ... public function fields(Request $request): array { return [ // ... Badge::make(__('credit_checks.rating.label'), 'rating', fn(CreditCheckRating $rating) => $rating->label()) ->map(CreditCheckRating::badgeColors()) ->sortable(), Select::make(__('credit_checks.rating.label'), 'rating') ->options(CreditCheckRating::optionsForSelect()) ->onlyOnForms(), // ... ]; } // ... }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.