How to return an array instead of a collection in Laravel? How to return an array instead of a collection in Laravel? laravel laravel

How to return an array instead of a collection in Laravel?


Just use pluck() and ->toArray():

Foo::where('b', 15)->pluck('a')->toArray();

Laravel's pluck() method.

Laravel's toArray() method.


The lists method is deprecated and pluck need some parameter so, if you want to get all the attributed in array format, use it this way.

Foo::select('a')->where('b', 15)->get()->all();


Do

Foo::where('b', 15)->lists('a')->all();

This will give you an array of ids. eg [2, 3, 5]