I'm using Laravel collective form builder for building forms. I have used select like this:
<div class="form-group">
{!! Form::label('module', 'Modul'); !!}
{!! Form::select('module_id', [$modules], $data->module_id) !!}
</div>
From the unknown reasons, the tag appears in my code. I don't want it there, but I'm really unable to find a simple way how to remove it. Thank you for a help
Reviewing the source code for the package and the tests we can see that the optgroup
is included if the array passed is multidimensional, you can see that within this test.
Reviewing the code you've provided we can see that you're creating a new array containing $modules
:
Form::select('module_id', [$modules], $data->module_id)
Which means that if $modules
is already an array, you're creating a multidimensional array. This is what the select()
method is receiving:
[
[
"a",
"b",
"c",
],
There's no key present so the array is keyed numerically, starting at 0, hence the optgroup
label
value is 0
. You should be passing a single-level array if you want to have a single level of options in your select, e.g:
[
"a",
"b",
"c",
]
You can fix this by not nesting your array in another array, [$modules]
becomes $modules
:
<div class="form-group">
{!! Form::label('module', 'Modul'); !!}
{!! Form::select('module_id', $modules, $data->module_id) !!}
</div>