peltonsolutions/filament-enums

A custom package for Filament for handling enum values in Filament Admin.

1.1.0 2024-03-14 16:14 UTC

This package is auto-updated.

Last update: 2024-05-14 16:41:44 UTC


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:

  1. Gives options from the map() function
  2. Makes the field required if it's not \PeltonSolutions\LaravelEnums\Models\NullableEnum
  3. 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.