I am using Laravel HTML component to create a dropdown to list all the groups to which an user can belong.
The list of groups comes from a Groups table.
Currently in my controller my code looks like
$groups = array();
$groupModels = Group::all(['id', 'name']);
foreach ($groupModels as $groupModel) {
$groups[$groupModel->id] = $groupModel->name;
}
return view('myview', compact('groups'));
and in my view I have the following code to create the dropdown
{!! Form::select('group', $groups, null, ['class' => 'form-control']) !!}
This works, but I am trying to see if there is a way to avoid the foreach
loop and directly convert the list of Models into an array. Is it possible?