1

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

1 Answer 1

5

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.