Select options are not loading along with afterStateHydrated(). whats wrong with my code?

Select options are not loading with model_ids when I'm using afterStateHydrated() to trim these model_ids and format correct JSON. These model_ids are array of foreign key and I want it to be saved as JSON like this "[253,256,255]". ~ Forms\Components\Select::make('model_id') ->label('Vehicle Models') ->options(function ($get) { $makeId = $get('make_id'); $models = \App\Models\VehicleModel::where('make_id', $makeId) ->pluck('model_name', 'id') ->toArray();
return $models; }) ->multiple()
->preload() ->afterStateHydrated(function ($state, callable $set) { try {
if (is_string($state)) { $state = stripslashes($state); $state = json_decode($state, true); }
if (!is_array($state)) { $state = []; }
if (count($state) === 1 && is_string($state[0])) {
$state = explode(',', str_replace('"', '', $state[0])); } $cleanedState = array_map(function ($item) { return preg_replace('/[^0-9]/', '', trim($item)); }, $state); $cleanedState = array_filter($cleanedState);
$mdlIds = json_encode(array_values($cleanedState)); $set('model_id', $mdlIds); } catch (\Exception $e) {
report($e); $set('model_id', '[]'); } }), ~
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?