laravel collection to array with/without keys

Collections to array on laravel is a easy to solve problem. An Laravel array to collection convert on Controller is a few ways.

You can use toArray() of eloquent as below. 

laravel collection to array without keys

The toArray method converts the collection into a plain PHP array. If the collection’s values are Eloquent models, the models will also be converted to arrays

Laravel Eloquent to Array Key Value

Eloquent to return an array using a column value as key




laravel eloquent to array key value 

$products = collect(DB::table('products')->get())->keyBy('name');


define a new array with laravel eloquent to array key value

$products = Product::all()->keyBy('name');

Sources: Laravel Documentation 8.0

Leave a Comment