laravel collection to array
Solution 1
You can use toArray() of eloquent as below.
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
$comments_collection = $post->comments()->get()->toArray()
From Laravel Docs:
toArray also converts all of the collection's nested objects that are an instance of Arrayable to an array. If you want to get the raw underlying array, use the all method instead.
Solution 2
Use all()
method - it's designed to return items of Collection:
/**
* Get all of the items in the collection.
*
* @return array
*/
public function all()
{
return $this->items;
}
Solution 3
Try this:
$comments_collection = $post->comments()->get()->toArray();
see this can help you
toArray() method in Collections
Solution 4
you can do something like this
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toArray();
Reference is https://laravel.com/docs/5.1/collections#method-toarray
Originally from Laracasts website https://laracasts.com/discuss/channels/laravel/how-to-convert-this-collection-to-an-array
Related videos on Youtube
datavoredan
Economist by training, Data Scientist by trade. Working towards being a more complete data scientist through learning about the best tools available. Working to become proficient in: Object Oriented Programming Python (pandas etc + Django) R SQL
Updated on February 10, 2022Comments
-
datavoredan about 2 years
I have two models,
Post
andComment
; many comments belong to a single post. I'm trying to access all comments associated with a post as an array.I have the following, which gives a collection.
$comments_collection = $post->comments()->get()
How would I turn this
$comments_collection
into an array? Is there a more direct way of accessing this array through eloquent relationships? -
Akshay Kulkarni over 6 yearsSometimes this method throws exception when there is no data.
-
Tijan Manandhar about 6 yearsCan you tell me the case where it throws an expection. I tried with null data but doesn't throw an exception
-
nevvermind about 5 yearsNit-pick: if the array elements implement
\Illuminate\Contracts\Support\Arrayable
, they will be converted into arrays, too, recursively. That includes Eloquent models. -
Sebastien C. over 4 yearsThis shouldn't be the top answer.
->toArray()
does not convert the collection to an array, it converts the whole contents into arrays, including the items of the collection.->all()
should be the accepted answer. -
Drudge Rajen over 4 years@SebastienC. OP had asked the way to convert collection to an array. So,
toArray()
is fine for that. Also, I have updated the answer with documentation. -
Kamlesh almost 3 yearsIf query does not have any record then toArray() does not work on NULL record and returns error.
-
Kamlesh almost 3 yearsIf query does not have any record then toArray() does not work on NULL record and returns error.
-
Kamlesh almost 3 yearsIf query does not have any record then toArray() does not work on NULL record and returns error.
-
Jovylle almost 3 yearsbut is it in array?
-
qwertynik almost 3 yearsYes @JovylleBermudez. It is an Array of Objects
-
PhillipMwaniki over 2 yearsDon't use
toArray()
. The returned array is more of a collection. The correct answer should be usingall()
-
Luciano Fantuzzi almost 2 yearsThis is the right solution for me since I need an array of models, not an array of arrays.