对自定义数组进行雄辩收集的最佳方法

Ben*_*ius 7 laravel eloquent laravel-4

是什么力量让最好的方式Object::all()array('object_id', 'object_name')?我需要一个很好的代码来为SELECT使用eloquent集合:{{ Form:select('objects', $custom_array) }}.for循环是唯一的方法吗?

Hol*_*eis 17

我想你正在寻找toArray():

User::all()->toArray();
Run Code Online (Sandbox Code Playgroud)

http://four.laravel.com/docs/eloquent#converting-to-arrays-or-json

要获得可以直接使用的数组,Form::select()可以使用以下命令:

$contacts = Contact::orderBy('name')->lists('name', 'id');
$contacts = count($contacts) > 0 ? $contacts : array();

{{ Form::select('contact', $contacts) }}
Run Code Online (Sandbox Code Playgroud)