please format your question for us to have a chance,
Jun 20, 2023
12
Level 1
Sort Records Inserting them in {{!! Form:select !!}}, using <optgroup>
Hello I have a table with the following form:
| id | root_id | name |
+-----+--------+------+
| 1 | 0 | Herds |
| 2 | 0 | Machinery |
| 3 | 0 | Structures |
| 4 | 2 | Massey Ferguson |
| 5 | 2 | Cremasco Máquinas Ltda |
| 6 | 2 | GM do Brasil |
| 7 | 2 | Caterpillar Inc. Brazil |
| 8 | 2 | Nogueira Ind. and with. SA |
| 9 | 1 | Fine Gold |
| 10 | 1 | Chemitec |
| 11 | 3 | Gerdau |
| 12 | 3 | MadeWest |
+-----+--------+----+
In it we find the fields 'root_id'. When 'root_id' equals 0(zero), it is 'optgroup'. When 'root_id' is different from 0(zero), it is 'option'.
How can I use '' in '{{!! Form:select !!}}', so that the listing is sorted by 'id' + 'root_id'? Is this possible in '{{!! Form:select !!}}', or should I program it in the Controller? or create a separate function?
If the understanding was not clear, it would appear like this in the 'select':
+-----+-------+-------+
| id | root_id | name |
+-----+--------+------+
| 1 | 0 | Herds |
| 9 | 1 | Fine Gold |
| 10 | 1 | Chemitec |
| 2 | 0 | Machinery |
| 7 | 2 | Caterpillar Inc. Brazil |
| 5 | 2 | Cremasco Máquinas Ltda |
| 6 | 2 | GM do Brasil |
| 4 | 2 | Massey Ferguson |
| 8 | 2 | Nogueira Ind. and with. SA |
| 3 | 0 | Structures |
| 11 | 3 | Gerdau |
| 12 | 3 | MadeWest |
+-----+--------+----+
it is possible?
Level 122
something like (adapt to the name of your model) and ditch that awful collective form element
<select>
@foreach($things->where('root_id',0) as $thing)
<optgroup label="{{ $thing->name }}">
@foreach($things->where('root_id', $thing->id) as $childthing)
<option>{{ $childthing->name }}</option>
@endforeach
</optgroup>
@endforeach
</select>
1 like
Please or to participate in this conversation.