Reputation: 3105
it should be simple but I am missing something, lets say this simple eloquent:
Post::select('id')->take(5)->get();
I want to get simple array with the results id's so it will look like this:
[1,2,3,4,5]
but i am getting something like this:
[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"}]
flatten() not working and I am getting the same results:
Post::select('id')->take(5)->get()->flatten();
http://laravel.com/docs/master/collections#method-flatten
The flatten method flattens a multi-dimensional collection into a single dimension:
what i am missing? I remember there is a short line laravel way of getting this results without iterate through the array and create a new one
Upvotes: 1
Views: 3998
Reputation: 3105
just got it, its the lists() that do the magic so the answer is:
Post::select('id')->take(5)->lists('id');
Update: as of laravel 5.2 lists() become deprecated
The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.
the new method name is pluck which work the same:
Post::select('id')->take(5)->pluck('id');
Upvotes: 5