peltonsolutions / filament-enums
A custom package for Filament for handling enum values in Filament Admin.
Requires
- php: ^8.2
- filament/filament: ^3.0.9
- laravel/framework: ^11.26
- peltonsolutions/laravel-enums: ^1.1
Requires (Dev)
- pestphp/pest: ^2.33
- pestphp/pest-plugin-livewire: ^2.1
README
filament-enums is an extension to the peltonsolutions/laravel-enums package, designed specifically for Laravel developers. This package simplifies working with enumerated values in your Filament applications, thus enhancing the readability and maintainability of your code.
Like the laravel-enums package, filament-enums supports the use of named constants, improving the organization and clarity of your code. It leverages the features provided by laravel-enums, complementing them with additional functionalities tailored for Filament.
Optionally, this package allows you to make your Enum fields nullable through the NullableEnum
class extension.
Install
You can install the package via composer using the following command:
composer require peltonsolutions/filament-enums
Example:
class ContentPageStatus extends \PeltonSolutions\LaravelEnums\Models\Enum { const DRAFT = 'draft'; const SCHEDULED = 'scheduled'; const PUBLISHED = 'published'; const ARCHIVED = 'archived'; public static function map(): array { return [ static::DRAFT => trans('content_page.statuses.draft'), static::SCHEDULED => trans('content_page.statuses.scheduled'), static::PUBLISHED => trans('content_page.statuses.published'), static::ARCHIVED => trans('content_page.statuses.archived'), ]; } }
class ContentPage extends Model { protected $casts = [ 'status' => ContentPageStatus::class ]; }
class ContentPageResource extends Resource { protected static ?string $model = ContentPage::class; public static function form(Form $form): Form { return $form ->schema([ \PeltonSolutions\FilamentEnums\Models\Forms\Components\EnumSelect::make('status') ->default(ContentPageStatus::DRAFT); } }
This will create a Select component that:
- Gives options from the
map()
function - Makes the field required if it's not
\PeltonSolutions\LaravelEnums\Models\NullableEnum
- Requires that the selected value matches one of the
map()
keys
Testing
To ensure that laravel-enums is functioning correctly, you can run the package's tests using:
NOTE: Tests are WIP. Having troubles writing tests that require the $record
to tbe passed into the closures.
composer test
Security
If you discover any security-related issues, please email security@peltonsolutions.com instead of using the issue tracker.
Credits
License
filament-enums is open-sourced software. It's licensed under the MIT license, which is a permissive license allowing the software to be used, modified, and shared.