laravel collection to array laravel collection to array laravel laravel

laravel collection to array


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.


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;}


Try this:

$comments_collection = $post->comments()->get()->toArray();

see this can help you
toArray() method in Collections