Filament Select Option

Could you kindly suggest an alternative approach that would be more effective than the current course of action? When I fetch the data it's weird as you can see the photo!
Forms\Components\Select::make('company_size')
->options([
'1_to_5' => '1 - 5',
'10_to_20' => '10 - 20',
'50_to_100' => '50 - 100',
'100_to_500' => '100 - 500',
'500_to_1000' => '500 - 1000',
'1000_and_more' => '1000+',
])
->label('Team Size')
->required(),
Forms\Components\Select::make('company_size')
->options([
'1_to_5' => '1 - 5',
'10_to_20' => '10 - 20',
'50_to_100' => '50 - 100',
'100_to_500' => '100 - 500',
'500_to_1000' => '500 - 1000',
'1000_and_more' => '1000+',
])
->label('Team Size')
->required(),
21 Replies
Dennis Koch
Dennis Koch2y ago
It's not weird. It is the value that you set as the key If you storing different data than you want to ouput, you need to map it back
Shaung Bhone
Shaung BhoneOP2y ago
How can I show like that in my front 100 - 500?
Dennis Koch
Dennis Koch2y ago
Map it back to your values. Via match or an array
Shaung Bhone
Shaung BhoneOP2y ago
Thank you
Cybrarist
Cybrarist2y ago
why not using enums ?
Patrick Boivin
This is a good use-case for an enum, check out the second example here : https://www.php.net/manual/en/language.enumerations.examples.php
Shaung Bhone
Shaung BhoneOP2y ago
Thank you
Forms\Components\Select::make('company_size')
->options([
TeamRange::getValues()
])
->label('Team Size')
->required(),
Forms\Components\Select::make('company_size')
->options([
TeamRange::getValues()
])
->label('Team Size')
->required(),
I can't use like this.
Patrick Boivin
Probably not... can you show us getValues()?
Shaung Bhone
Shaung BhoneOP2y ago
Filament\Forms\Components\Select::isOptionDisabled(): Argument #2 ($label) must be of type string, array given, called in /var/www/html/storage/framework/views/1de72626fd42a8d8e29bb5f2fcead228.php on line 80
Filament\Forms\Components\Select::isOptionDisabled(): Argument #2 ($label) must be of type string, array given, called in /var/www/html/storage/framework/views/1de72626fd42a8d8e29bb5f2fcead228.php on line 80
$options = TeamRange::getValues();
$teamSizeOptions = array_map(function ($value) {
return [
'value' => $value,
'label' => TeamRange::getDescription($value),
];
}, $options);
$options = TeamRange::getValues();
$teamSizeOptions = array_map(function ($value) {
return [
'value' => $value,
'label' => TeamRange::getDescription($value),
];
}, $options);
Forms\Components\Select::make('company_size')
->options($teamSizeOptions)
->label('Team Size')
->required(),
Forms\Components\Select::make('company_size')
->options($teamSizeOptions)
->label('Team Size')
->required(),
<?php declare(strict_types=1);

namespace App\Enums;

use BenSampo\Enum\Enum;

final class TeamRange extends Enum
{
const ONE_TO_FIVE = '1_to_5';
const TEN_TO_TWENTY = '10_to_20';
const FIFTY_TO_HUNDRED = '50_to_100';
const HUNDRED_TO_FIVE_HUNDRED = '100_to_500';
const FIVE_HUNDRED_TO_THOUSAND = '500_to_1000';
const THOUSAND_AND_MORE = '1000_and_more';

public static function getDescription($value): string
{
switch ($value) {
case self::ONE_TO_FIVE:
return '1 - 5';
case self::TEN_TO_TWENTY:
return '10 - 20';
case self::FIFTY_TO_HUNDRED:
return '50 - 100';
case self::HUNDRED_TO_FIVE_HUNDRED:
return '100 - 500';
case self::FIVE_HUNDRED_TO_THOUSAND:
return '500 - 1000';
case self::THOUSAND_AND_MORE:
return '1000+';
default:
return parent::getDescription($value);
}
}
}
<?php declare(strict_types=1);

namespace App\Enums;

use BenSampo\Enum\Enum;

final class TeamRange extends Enum
{
const ONE_TO_FIVE = '1_to_5';
const TEN_TO_TWENTY = '10_to_20';
const FIFTY_TO_HUNDRED = '50_to_100';
const HUNDRED_TO_FIVE_HUNDRED = '100_to_500';
const FIVE_HUNDRED_TO_THOUSAND = '500_to_1000';
const THOUSAND_AND_MORE = '1000_and_more';

public static function getDescription($value): string
{
switch ($value) {
case self::ONE_TO_FIVE:
return '1 - 5';
case self::TEN_TO_TWENTY:
return '10 - 20';
case self::FIFTY_TO_HUNDRED:
return '50 - 100';
case self::HUNDRED_TO_FIVE_HUNDRED:
return '100 - 500';
case self::FIVE_HUNDRED_TO_THOUSAND:
return '500 - 1000';
case self::THOUSAND_AND_MORE:
return '1000+';
default:
return parent::getDescription($value);
}
}
}
Patrick Boivin
Try this instead:
$teamSizeOptions = collect($options)
->mapWithKeys(fn ($value) => [$value => TeamRange::getDescription($value)])
->all();
$teamSizeOptions = collect($options)
->mapWithKeys(fn ($value) => [$value => TeamRange::getDescription($value)])
->all();
Shaung Bhone
Shaung BhoneOP2y ago
Thank you got it Pardon I can't fetch to front
Cannot construct an instance of TeamRange using the value (string) `10 - 50`. Possible values are [1_to_5, 10_to_20, 50_to_100, 100_to_500, 500_to_1000, 1000_and_more].
Cannot construct an instance of TeamRange using the value (string) `10 - 50`. Possible values are [1_to_5, 10_to_20, 50_to_100, 100_to_500, 500_to_1000, 1000_and_more].
dd($job->company_size->description)
// result
"10 - 50"
dd($job->company_size->description)
// result
"10 - 50"
Patrick Boivin
This looks ok, what is causing the error above?
Shaung Bhone
Shaung BhoneOP2y ago
I got this error when I'm not dd.
Patrick Boivin
I'm not familiar with the package you are using... (BenSampo\Enum) Can you show me the code that is causing the error, with more context than just $job->company_size->description
Shaung Bhone
Shaung BhoneOP2y ago
Actually that's it.
Kenneth Sese
Kenneth Sese2y ago
Can you be sure your sent the correct code for your enum class? The error says it’s looking for “10 - 50”, but that’s nowhere to be found in the code you sent. Where did it get that value from? It appears to come from TeamRange::getValues(), but that value isn’t in the code you sent.
Shaung Bhone
Shaung BhoneOP2y ago
That's it
Kenneth Sese
Kenneth Sese2y ago
Weird. Where is it pulling “10 - 50” from? Can you search your project for that string? I think your options array isn't formed correctly. Try this:
->options(collect(TeamRange::getValues())->mapWithKeys(fn ($value) => [$value => TeamRange::getDescription($value)])),
->options(collect(TeamRange::getValues())->mapWithKeys(fn ($value) => [$value => TeamRange::getDescription($value)])),
Shaung Bhone
Shaung BhoneOP2y ago
Thank you let me check
<?php

namespace App\Enums;

enum TeamRange: int
{
case ONE_TO_FIVE = 1;
case TEN_TO_TWENTY = 2;
case FIFTY_TO_HUNDRED = 3;
case HUNDRED_TO_FIVE_HUNDRED = 4;
case FIVE_HUNDRED_TO_THOUSAND = 5;
case THOUSAND_AND_MORE = 6;

public function getName(): string
{
return match ($this) {
self::ONE_TO_FIVE => '1 - 5',
self::TEN_TO_TWENTY => '10 - 20',
self::FIFTY_TO_HUNDRED => '50 - 100',
self::HUNDRED_TO_FIVE_HUNDRED => '100 - 500',
self::FIVE_HUNDRED_TO_THOUSAND => '500 - 1000',
self::THOUSAND_AND_MORE => '1000+',
default => 'No team!',
};
}
}
<?php

namespace App\Enums;

enum TeamRange: int
{
case ONE_TO_FIVE = 1;
case TEN_TO_TWENTY = 2;
case FIFTY_TO_HUNDRED = 3;
case HUNDRED_TO_FIVE_HUNDRED = 4;
case FIVE_HUNDRED_TO_THOUSAND = 5;
case THOUSAND_AND_MORE = 6;

public function getName(): string
{
return match ($this) {
self::ONE_TO_FIVE => '1 - 5',
self::TEN_TO_TWENTY => '10 - 20',
self::FIFTY_TO_HUNDRED => '50 - 100',
self::HUNDRED_TO_FIVE_HUNDRED => '100 - 500',
self::FIVE_HUNDRED_TO_THOUSAND => '500 - 1000',
self::THOUSAND_AND_MORE => '1000+',
default => 'No team!',
};
}
}
I removed the package and it's working now.
Patrick Boivin
Nice and simple!
Shaung Bhone
Shaung BhoneOP2y ago
Thanks I changed to these.
protected function getTeamRangeOptions(): array
{
return \Illuminate\Support\Arr::map(
TeamRange::cases(), fn($enum) => $enum->getName()
);
}
protected function getTeamRangeOptions(): array
{
return \Illuminate\Support\Arr::map(
TeamRange::cases(), fn($enum) => $enum->getName()
);
}
Forms\Components\Select::make('company_size')
->options($this->getTeamRangeOptions())
->label('Team Size')
->required(),
Forms\Components\Select::make('company_size')
->options($this->getTeamRangeOptions())
->label('Team Size')
->required(),
Want results from more Discord servers?
Add your server