@php
$toString = function ($item) use (&$toString) {
if ($item instanceof \Illuminate\Support\Collection) {
$item = $item->toArray();
}
if (is_object($item) && isset($item->value)) {
$item = $item->value;
}
if ($item instanceof \Illuminate\Support\Collection) {
$item = $item->toArray();
}
if (is_array($item)) {
return array_map(fn($x) => $toString($x), $item);
}
if ($item === null) {
return null;
}
if (is_bool($item)) {
return $item ? '1' : '0';
}
if (is_scalar($item) || $item instanceof \Stringable) {
return (string) $item;
}
if (is_object($item) && isset($item->value)) {
return $toString($item->value);
}
return '';
};
if (isset($name)) {
$oldKey = $toString($name);
$oldKey = is_array($oldKey) ? '' : ($oldKey ?? '');
if (substr($oldKey, -2) === '[]') {
$oldKey = substr($oldKey, 0, -2);
}
$selected = old($oldKey, $selected ?? null);
}
$fieldName = $toString(($name ?? $uuid) ?? '');
$fieldName = is_array($fieldName) ? '' : ($fieldName ?? '');
if (isset($options) && $options instanceof \Illuminate\Support\Collection) {
$options = $options->toArray();
}
if (isset($options) && is_array($options)) {
$normalizedOptions = [];
foreach ($options as $k => $v) {
$key = $toString($k);
$label = $toString($v);
$normalizedOptions[is_array($key) ? '' : ($key ?? '')] = is_array($label) ? '' : ($label ?? '');
}
$options = $normalizedOptions;
}
if (isset($value)) {
$value = $toString($value);
}
if (isset($selected)) {
$selected = $toString($selected);
}
if (isset($default)) {
$default = $toString($default);
}
$currentValue = $value ?? ($selected ?? ($default ?? null));
if ($multiple) {
if (!is_array($currentValue)) {
$currentValue = ($currentValue === null || $currentValue === '') ? [] : [$currentValue];
}
$currentValue = array_map(fn($x) => $toString($x), $currentValue);
$currentValue = array_map(fn($x) => is_array($x) ? '' : ($x ?? ''), $currentValue);
} else {
$currentValue = $toString($currentValue);
$currentValue = is_array($currentValue) ? null : $currentValue;
}
$defaultValueAttribute = is_array($default ?? null)
? implode(',', array_map(fn($x) => is_array($x) ? '' : ($x ?? ''), $default))
: (($default ?? null) === null ? '' : (is_array($default) ? '' : $default));
@endphp
@if(isset($type) && $type === 'radio')
@if(isset($options))
@foreach($options as $k => $v)
@php
$kStr = $toString($k);
$kStr = is_array($kStr) ? '' : ($kStr ?? '');
$vHtml = $toString($v);
$vHtml = is_array($vHtml) ? '' : ($vHtml ?? '');
$vText = strip_tags($vHtml);
$optId = (($id ?? $uuid) ?: $uuid) . '_' . $loop->index;
$isChecked = $multiple ? in_array($kStr, $currentValue ?? [], true) : ($currentValue !== null && $currentValue === $kStr);
@endphp
@endforeach
@endif
@else
@endif