deciorocha's avatar

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?

0 likes
12 replies
Snapey's avatar

please format your question for us to have a chance,

1 like
deciorocha's avatar

@Snapey I am not able to format my tables. Could you help me with the formatting? Where do I find the to format the table? Is there any post about it? I apologize for my lack of knowledge...

Snapey's avatar

@deciorocha there is a link at the bottom of the comment form and when creating question.

Basically three backticks ``` on their own line before and after the code block

Snapey's avatar
Snapey
Best Answer
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
deciorocha's avatar

@Snapey What a disappointment! I really liked these elements. Can I assume they are out of favor then?

Thank you for your help and attention to solve my problem. Your example is exactly how I do it... Thank you very much!

krisi_gjika's avatar

@deciorocha why not create a relation between root elements and their children, that way you can call:

OptGroups::query()->where('root_id', 0)->with('options')->get();

I would make root_id -> null for $optGroups instead of 0 tho. Could also make a scope as

public function scopeIsOptGroup(Builder $query)
{
	return $query->whereNull('root_id');
}
1 like
deciorocha's avatar

@krisi_gjika Interesting! I found it practical and elegant. Forgive me for my ignorance, but what would "root_id -> null" be better than "root_id = 0"?

krisi_gjika's avatar

@deciorocha I assume root_id is not a foreign key, since id:0 does not exist. With a nullable root_id field, you can update the field as a foreign key. So better/faster queries, foreign key checks, etc.

1 like
deciorocha's avatar

@krisi_gjika I have little knowledge about it. I'm still a novice. I will study the matter. Thanks for the tip.

Snapey's avatar

please mark it answered if you have a solution.

Collective forms is not in favour and at this point is just about abandoned project

1 like

Please or to participate in this conversation.