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 ?
So to get an Array of Eloquent models you need to use SomeModel::all ()->all (); Show activity on this post. My first thought was $collection->toArray () but that also converts the Eloquent models to arrays. But the docs say that $collection->all () should avoid that. toArray also converts all of the collection's nested objects to an array.
Laravel Eloquent collection object extends the base collection, so it naturally inherits the dozens of methods used to work with an underlying array of Eloquent models fluently.
Once you have defined the newCollection method, you will receive an instance of your custom collection anytime Eloquent returns the collection instance of that model.
All multi-result sets returned by Eloquent are instances of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship.
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) }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With