php - Convert collection of models into an array with id as key -
i using laravel html component create dropdown list groups user can belong.
the list of groups comes groups table.
currently in controller code looks like
    $groups = array();     $groupmodels = group::all(['id', 'name']);      foreach ($groupmodels $groupmodel) {         $groups[$groupmodel->id] = $groupmodel->name;     }      return view('myview', compact('groups')); and in view have following code create dropdown
    {!! form::select('group', $groups, null, ['class' => 'form-control']) !!} this works, trying see if there way avoid foreach loop , directly convert list of models array. possible?
use pluck() method:
$groups = group::pluck('name', 'id'); return view('myview', compact('groups')); 
Comments
Post a Comment