Benas Radzevicius
Benas Radzevicius

Reputation: 377

The best way to make eloquent collection to custom array

What is the best way to make Object::all() to array('object_id', 'object_name')? I need a nice code to use eloquent collection for SELECT: {{ Form:select('objects', $custom_array) }}. Is a for loop the only way to do that ?

Upvotes: 7

Views: 10933

Answers (1)

Holger Weis
Holger Weis

Reputation: 1695

I think you are looking for toArray():

User::all()->toArray();

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

To get an array that can be directly used with Form::select(), you can use the following:

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

{{ Form::select('contact', $contacts) }}

Upvotes: 17

Related Questions