Select Filter for Enums

Is there a way to filter my AppointmentStatusEnums so it will only show Pending, Approved and Completed?
No description
7 Replies
awcodes
awcodes5mo ago
If your model has the appropriate cast for the enum, this should just work. What exactly is the issue you’re encountering?
NothingToSay
NothingToSay5mo ago
no issue Currently, It displays all seven statuses, but I want it to show only the statuses 'Pending,' 'Approved,' and 'Completed'.
awcodes
awcodes5mo ago
You would need to manually set the options as an array of those enum values then.
fabioferreira
fabioferreira5mo ago
I have an appended field that returns an Enum in my Model. The SelectFilter doesn't work on those cases. Is there a different way to filter appended fields?
Jr.Pikong
Jr.Pikong5mo ago
Maybe you could implement it like this: ->options(collect(Status::cases())->where('name', '!=', 'IN_PROCESS')->pluck('name', 'value')->toArray()), and of course, you can use whereIn() as well.
Jr.Pikong
Jr.Pikong5mo ago
Laravel News
Increment a Rate Limiter by a Custom Amount in Laravel 10.46 - Lara...
The Laravel team released v10.46 this week with new Enum validation methods, incrementing a rate limiter by a custom amount, Conditionable Enum validation rules, and more.
TK
TK4w ago
@NothingToSay in addition to customising a SelectFilter, when using Enums on a relationship, the default SelectFilter won't work. Filament tries to use the Enum in the where clause in Eloquent as a string. So, in order to use the Enum in a relationship, you need to build your own, just using ->options() :
Filter::make('type')
->form([
Select::make('accommodation.type')
->options(AccommodationTypeEnum::class) // <-- just use the Enum class
->native(false)
->searchable()
->preload(),
])
->query(
function (Builder $query, array $data): Builder {
return $query->when(
data_get($data, 'accommodation.type'),
function (Builder $query, $value): Builder {
return $query->whereRelation('accommodation', 'type', '=', $value); // <-- necessary, since Filament has an Enum class and not the value!
},
);
}
)
->indicateUsing(function (array $data): ?string {
$accommodationType = data_get($data, 'accommodation.type');

if (empty($accommodationType)) {
return null;
}

return 'Accommodation Type: ' . AccommodationTypeEnum::from($accommodationType)->getLabel();
}),
Filter::make('type')
->form([
Select::make('accommodation.type')
->options(AccommodationTypeEnum::class) // <-- just use the Enum class
->native(false)
->searchable()
->preload(),
])
->query(
function (Builder $query, array $data): Builder {
return $query->when(
data_get($data, 'accommodation.type'),
function (Builder $query, $value): Builder {
return $query->whereRelation('accommodation', 'type', '=', $value); // <-- necessary, since Filament has an Enum class and not the value!
},
);
}
)
->indicateUsing(function (array $data): ?string {
$accommodationType = data_get($data, 'accommodation.type');

if (empty($accommodationType)) {
return null;
}

return 'Accommodation Type: ' . AccommodationTypeEnum::from($accommodationType)->getLabel();
}),
Thought it would be nice to share this solution somewhere, since I had this issue in the last 2 hours...
Want results from more Discord servers?
Add your server
More Posts