Support the ongoing development of Laravel.io →
Eloquent Views Forms

Suppose you have a simple products table:

id | name  | category
.......................
1  | prod1 | first
2  | prod2 | first
3  | prod3 | second
4  | prod4 | third
5  | prod5 | third

Note, that in this example "category" is a plain string, not a foreign key.

I've been trying to figure out if it's possible to render Form::select with optgroups basing on "category" field. I didn't find a ready to use method (like Collection::lists()), so I've written a little Form macro:

https://gist.github.com/marekmurawski/8855132

These macros would render selects with optgroups basing on the example table's category like this:

first
  prod1
  prod2
second
  prod3
third
  prod4
  prod5
Last updated 3 years ago.
0

Thanks, very handy! That should be worthily included in the FormBuilder class.
Btw, though common practice, it is not mandatory that a foreign key is an integer, it could indeed be a string, that's not a shame.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.