laravel collection to array

209,915

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

Share:
209,915

Related videos on Youtube

datavoredan
Author by

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, 2022

Comments

  • datavoredan
    datavoredan about 2 years

    I have two models, Post and Comment; 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
    Akshay Kulkarni over 6 years
    Sometimes this method throws exception when there is no data.
  • Tijan Manandhar
    Tijan Manandhar about 6 years
    Can you tell me the case where it throws an expection. I tried with null data but doesn't throw an exception
  • nevvermind
    nevvermind about 5 years
    Nit-pick: if the array elements implement \Illuminate\Contracts\Support\Arrayable, they will be converted into arrays, too, recursively. That includes Eloquent models.
  • Sebastien C.
    Sebastien C. over 4 years
    This 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
    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
    Kamlesh almost 3 years
    If query does not have any record then toArray() does not work on NULL record and returns error.
  • Kamlesh
    Kamlesh almost 3 years
    If query does not have any record then toArray() does not work on NULL record and returns error.
  • Kamlesh
    Kamlesh almost 3 years
    If query does not have any record then toArray() does not work on NULL record and returns error.
  • Jovylle
    Jovylle almost 3 years
    but is it in array?
  • qwertynik
    qwertynik almost 3 years
    Yes @JovylleBermudez. It is an Array of Objects
  • PhillipMwaniki
    PhillipMwaniki over 2 years
    Don't use toArray(). The returned array is more of a collection. The correct answer should be using all()
  • Luciano Fantuzzi
    Luciano Fantuzzi almost 2 years
    This is the right solution for me since I need an array of models, not an array of arrays.