|
4 | 4 |
|
5 | 5 | $field['value'] = old_empty_or_null($field['name'], '') ?? $field['value'] ?? $field['default'] ?? '';
|
6 | 6 |
|
| 7 | + $allowMultipleValues = $field['multiple'] ?? false; |
| 8 | +
|
7 | 9 | $possible_values = (function() use ($entity_model, $field) {
|
8 | 10 | $fieldName = $field['baseFieldName'] ?? $field['name'];
|
9 | 11 | // if developer provided the options, use them, no need to guess.
|
|
16 | 18 | $options = $entity_model::getPossibleEnumValues($fieldName);
|
17 | 19 | return array_combine($options, $options);
|
18 | 20 | }
|
19 |
| -
|
| 21 | + |
20 | 22 | // developer can provide the enum class so that we extract the available options from it
|
21 | 23 | $enumClassReflection = isset($field['enum_class']) ? new \ReflectionEnum($field['enum_class']) : false;
|
22 | 24 |
|
23 | 25 | if(! $enumClassReflection) {
|
24 | 26 | // check for model casting
|
25 | 27 | $possibleEnumCast = (new $entity_model)->getCasts()[$fieldName] ?? false;
|
26 |
| - if($possibleEnumCast && class_exists($possibleEnumCast)) { |
27 |
| - $enumClassReflection = new \ReflectionEnum($possibleEnumCast); |
| 28 | +
|
| 29 | + if($possibleEnumCast) { |
| 30 | + if(class_exists($possibleEnumCast)) { |
| 31 | + $enumClassReflection = new \ReflectionEnum($possibleEnumCast); |
| 32 | + }else{ |
| 33 | + // It's an `AsEnumCollection` cast |
| 34 | + if(str_contains($possibleEnumCast, ':')) { |
| 35 | + $enumClassReflection = new \ReflectionEnum(explode(':', $possibleEnumCast)[1]); |
| 36 | + $allowMultipleValues = true; |
| 37 | + } |
| 38 | + } |
28 | 39 | }
|
29 | 40 | }
|
30 | 41 |
|
|
55 | 66 | return array_combine($options, $options);
|
56 | 67 | })();
|
57 | 68 |
|
58 |
| -
|
59 |
| - if(function_exists('enum_exists') && !empty($field['value']) && $field['value'] instanceof \UnitEnum) { |
60 |
| - $field['value'] = $field['value'] instanceof \BackedEnum ? $field['value']->value : $field['value']->name; |
| 69 | + if(function_exists('enum_exists') && !empty($field['value'])) { |
| 70 | + match(true) { |
| 71 | + $field['value'] instanceof \UnitEnum => $field['value'] = $field['value'] instanceof \BackedEnum ? $field['value']->value : $field['value']->name, |
| 72 | + $field['value'] instanceof Illuminate\Support\Collection => $field['value'] = $field['value']->map(function($item) { |
| 73 | + return $item instanceof \UnitEnum ? $item instanceof \BackedEnum ? $item->value : $item->name : $item; |
| 74 | + })->toArray(), |
| 75 | + default => null |
| 76 | + }; |
| 77 | + $field['value'] = is_array($field['value']) ? $field['value'] : [$field['value']]; |
61 | 78 | }
|
62 | 79 | @endphp
|
63 | 80 |
|
64 | 81 | @include('crud::fields.inc.wrapper_start')
|
65 | 82 | <label>{!! $field['label'] !!}</label>
|
66 | 83 | @include('crud::fields.inc.translatable_icon')
|
67 | 84 | <select
|
68 |
| - name="{{ $field['name'] }}" |
| 85 | + name="{{ $allowMultipleValues ? $field['name'].'[]' : $field['name'] }}" |
69 | 86 | @include('crud::fields.inc.attributes', ['default_class' => 'form-control form-select'])
|
| 87 | + @if ($allowMultipleValues) |
| 88 | + multiple |
| 89 | + @endif |
70 | 90 | >
|
71 | 91 |
|
72 | 92 | @if ($entity_model::isColumnNullable($field['name']))
|
|
76 | 96 | @if (count($possible_values))
|
77 | 97 | @foreach ($possible_values as $key => $possible_value)
|
78 | 98 | <option value="{{ $key }}"
|
79 |
| - @if ($field['value']==$key) |
| 99 | + @if (in_array($key, $field['value'])) |
80 | 100 | selected
|
81 | 101 | @endif
|
82 | 102 | >{{ $possible_value }}</option>
|
|
0 commit comments