@props([ 'name', 'options' => [], 'value' => null, 'placeholder' => 'Pilih...', 'multiple' => false, 'search' => false, 'errorsBag' => null, 'showErrorSpace' => true, // NEW 'type' => 'default', ]) @php $oldValue = old($name); if ($oldValue === null) { $oldValue = $value ?? ($multiple ? [] : null); } // ✅ Normalize selected values (0-safe) if ($multiple) { $selectedValues = collect( is_array($oldValue) ? $oldValue : ($oldValue !== null ? explode(',', (string) $oldValue) : []) ) ->map(fn ($v) => (string) $v) ->filter(fn ($v) => $v !== null && $v !== ''); // 🔥 tidak buang "0" } else { $selectedValues = collect([(string) $oldValue]) ->filter(fn ($v) => $v !== null && $v !== ''); // 🔥 tidak buang "0" } // error handling $bag = $errorsBag ?? $errors ?? session('errors'); $hasError = $bag && $bag->has($name); // normalize options (punyamu sudah bagus, biarkan) if ($options instanceof \Illuminate\Support\Collection) { $options = $options->mapWithKeys(function ($item, $key) { if (is_array($item)) { $value = $item['id'] ?? $key; $label = $item['name'] ?? $value; } elseif (is_object($item)) { $value = $item->id ?? $key; $label = $item->name ?? $value; } else { $value = $key; $label = $item; } return [(string) $value => $label]; })->toArray(); } elseif (is_array($options)) { $normalized = []; foreach ($options as $key => $item) { if (is_array($item)) { $value = (string) ($item['id'] ?? $key); $label = $item['name'] ?? $value; } elseif (is_object($item)) { $value = (string) ($item->id ?? $key); $label = $item->name ?? $value; } else { $value = (string) $key; $label = $item; } $normalized[$value] = $label; } $options = $normalized; } @endphp