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

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -