In laravel 10 / filamentphp 3 app I have 3 related country/state/region fields referencing table :
CREATE TABLE `geo` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`parent_id` int DEFAULT NULL,
`left` int DEFAULT NULL,
`right` int DEFAULT NULL,
`depth` int NOT NULL DEFAULT '0',
`name` char(60) COLLATE utf8mb4_unicode_ci NOT NULL,
`alternames` text COLLATE utf8mb4_unicode_ci NOT NULL,
`country` char(2) COLLATE utf8mb4_unicode_ci NOT NULL,
`a1code` varchar(25) COLLATE utf8mb4_unicode_ci NOT NULL,
`level` char(10) COLLATE utf8mb4_unicode_ci NOT NULL,
`population` bigint NOT NULL,
`lat` decimal(9,6) NOT NULL,
`long` decimal(9,6) NOT NULL,
`timezone` char(30) COLLATE utf8mb4_unicode_ci NOT NULL,
When user selects country, then all states are filled correctly, but the simalar functionality
does not work when I need to fill all regions (‘region’ selection input) by selected state(‘state_id’ selection input) :
->schema([
Select::make('country')->label('Country')
->prefixIcon(getLocationIcon())
->preload()
->live(onBlur: true)
->options(ProfileHelper::getCountrySelectionItems(false))
/*returned array :
[en] => USA
[es] => Spain
[ua] => Ukraine
*/
->default(ProfileHelper::getDefaultCountry())
->helperText('Select one countries which are used in the app '),
/* BY SELECTING state OF country above list of regions in select below is autofilled */
Select::make('state_id')->label('State 987') // THIS SELECTION IS FILLED CORRECTLY
->options(function (callable $get) {
if (empty($get('country'))) { // THAT IS VALID COUNTRY
return [];
}
return Geo::getByCountry($get('country'))->getByLevel('ADM1') ->orderBy('name')->pluck('name', 'id');
})
->helperText('Select state of the country selected above'),
Select::make('region')->label('region')
->options(function (callable $get) {
Log::info(varDump($get('state_id'), ' -145 $get(state_id)::')); // BUT THIS VALUE IS ALWAYS NULL
if (empty($get('state_id'))) {
return [];
}
// SO THE SELEXCTION BELOW IS NOT AUTOFILLED!
return Geo::getByCountry($get('country'))->getParentId($get('state_id'))->getByLevel('ADM2')->orderBy('name')->get();
})
->helperText('Select region of the state selected above'),
TextInput::make('town')
->maxLength(100),
TextInput::make('postal_code')
->maxLength(6),
])
What can be wrong in my code ?