Convert a collection of models to an array of models

Convert a collection of models to an array of models

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting an Eloquent Collection to a Native Array

When working with Laravel's Eloquent ORM, you will frequently encounter a distinction between Collection instances and native PHP arrays. If you execute $post->comments()->get(), you are not retrieving a raw database result set; you are receiving an instance of Illuminate\Database\Eloquent\Collection. While this object behaves like an array in many contexts, it carries additional metadata and methods specific to Eloquent models. Sometimes, however, your downstream logic requires a standard PHP array.

To convert $comments_collection into a native array, the most direct method is calling the toArray() method. This approach triggers Eloquent's model casting mechanism, ensuring that every item in the collection remains an instance of the Comment model rather than a simple stdClass object or raw data array.

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

// Accessing items as standard models
foreach ($commentsArray as $comment) {
    // $comment is still an Eloquent Comment Model
    echo $comment->author;
}

There is often a desire to access this data without the overhead of the Collection wrapper entirely, perhaps to reduce memory usage or because you need to pass the data to a non-Eloquent component. While toArray() solves the immediate conversion, there are nuances regarding how Eloquent handles relationships and eager loading that affect the final structure of your array.

If you simply iterate over the collection using foreach, Laravel efficiently yields each model one by one. This is generally preferred for memory efficiency compared to converting the entire collection to an array upfront if you only need to process a subset of comments. However, if your application logic strictly demands a native array—such as when passing data to a function that does not accept Eloquent collections or when preparing data for JSON serialization where raw arrays are expected—you must explicitly invoke toArray().

A critical consideration with toArray() is the depth of conversion. By default, this method converts the collection itself into an array and casts every model within it. However, if your Comment models contain relationships (like a relationship back to the User or another nested relationship), those related models will also be converted into plain arrays unless you specify otherwise. This behavior can significantly impact memory consumption for large datasets or complex tree structures.

You can control this behavior using the $options array passed to toArray(). For instance, setting the 'serialize' option to false prevents deep serialization of related attributes. Conversely, if you want to keep related models as instances while only converting the top-level collection, you might need a custom cast or a different approach depending on your specific version of Laravel.

// Convert only the collection wrapper, keep relations as models (Laravel 9+)
$commentsWithRelations = $post->comments()->get()->toArray(
    ['serialize' => false] 
);

In scenarios where you are building a response for an API and need to flatten the entire structure into a single array of primitives, you might instead use pluck() or values(). pluck() is useful if you only need specific columns from the models, reducing payload size. For example, $post->comments()->get()->pluck('content') returns an array containing just the content strings. This is significantly lighter than converting the full model instances to arrays.

If you are dealing with large collections where memory is a constraint, consider using chunking or cursor pagination before converting to an array. However, for standard application logic where performance is not a bottleneck due to data volume, toArray() remains the cleanest and most readable solution. It preserves the rich type information of your models while satisfying the requirement for a native array structure.

Our network