M
MeshWorld.
Laravel Collections PHP Eloquent 5 min read

Create a Laravel Collection from an Array

Vishnu
By Vishnu
| Updated: Mar 27, 2026

collect() wraps any PHP array in a Collection instance — giving you all of Laravel’s chainable methods without touching the database. It works with plain arrays, API responses, or any structured data you have in memory. You don’t need an Eloquent model. You don’t need a database query. Just pass the array in, and all of Laravel’s collection methods are available immediately.

:::note[TL;DR]

  • collect($array) is the shortest path to a Collection instance
  • new Collection($array) is the explicit class-based alternative
  • Both work identically — collect() is just a global helper for the same thing
  • Chain filter(), map(), sum(), groupBy(), and 100+ other methods right away
  • No Eloquent or DB connection required :::

How do you create a Collection from a plain PHP array?

Two ways. The first is the global collect() helper — the one you’ll use 95% of the time:

$languages = ['Gujarati', 'Hindi', 'Sanskrit', 'Tamil', 'Urdu'];

$collection = collect($languages);

The second is direct class instantiation. You’ll see this in libraries and packages that avoid global helpers:

use Illuminate\Support\Collection;

$collection = new Collection(['Gujarati', 'Hindi', 'Sanskrit', 'Tamil', 'Urdu']);

Both produce an identical Illuminate\Support\Collection instance. Pick whichever reads better in context.

The scenario: You hit a third-party REST API and get back a JSON-decoded PHP array of products. You could loop over it with foreach and build the result manually — or you could wrap it in collect() and use filter(), map(), and groupBy() in three lines. The API doesn’t know about Eloquent. That doesn’t matter.

How do you chain methods right after creating a Collection?

You don’t have to assign the collection to a variable first. Chain directly off collect():

$total = collect([15.99, 9.49, 4.99, 22.00])->sum();
// 52.47

That’s the point. Once you’re in collection-land, you stay there.

What can you do with filter() and map() on an array-backed Collection?

filter() keeps only the items where your callback returns true. Here the prices array is filtered to items over $10, then totalled:

$prices = [15.99, 9.49, 4.99, 22.00, 7.50, 18.75];

$highValueTotal = collect($prices)
    ->filter(fn($price) => $price > 10)
    ->sum();

// Result: 56.74

map() transforms each item and returns a new Collection. This example pulls just the name from each product array:

$products = [
    ['name' => 'Keyboard', 'price' => 79.00],
    ['name' => 'Mouse',    'price' => 29.00],
    ['name' => 'Monitor',  'price' => 349.00],
];

$names = collect($products)
    ->map(fn($product) => $product['name']);

// Collection: ['Keyboard', 'Mouse', 'Monitor']

How do you group a plain array with groupBy()?

groupBy() organises items by a key or a callback. This is the method that saves the most boilerplate compared to a manual foreach:

$orders = [
    ['status' => 'pending',   'amount' => 50],
    ['status' => 'completed', 'amount' => 120],
    ['status' => 'pending',   'amount' => 30],
    ['status' => 'completed', 'amount' => 80],
];

$grouped = collect($orders)->groupBy('status');

// Result:
// 'pending'   => [['status' => 'pending', 'amount' => 50], [...]]
// 'completed' => [['status' => 'completed', 'amount' => 120], [...]]

The scenario: You fetch order data from an external API. It comes back as a flat array. You need to display orders in groups on the checkout summary page. groupBy() on a collect()-wrapped array does this in one line — no need for a DB query, no need for a second Eloquent relationship.

Does collect() work with associative arrays?

Yes. Keys are preserved. Methods like keys(), values(), and get() work as expected:

$config = [
    'timeout' => 30,
    'retries' => 3,
    'base_url' => 'https://api.example.com',
];

$timeout = collect($config)->get('timeout'); // 30
$keys    = collect($config)->keys()->all();  // ['timeout', 'retries', 'base_url']

Summary

  • collect($array) and new Collection($array) are equivalent. Use whichever fits your style.
  • You can chain filter(), map(), sum(), groupBy(), and any other collection method immediately after wrapping.
  • Array-backed collections are ideal for API responses and in-memory data that doesn’t come from Eloquent.
  • Key order is preserved. Associative arrays work without any extra configuration.
  • No database connection is required — these are pure PHP operations.

FAQ

Is there a performance difference between collect() and new Collection()? None. collect() is literally a global helper that calls new Collection() internally. The compiled result is identical.

Can I convert a Collection back to a plain array? Yes. Call ->all() to get the raw array, or ->toArray() for a recursive conversion (useful for nested collections).

Does collect() work with objects? Yes. You can pass an array of objects or even a single object (it’ll be treated as an array with one item). Methods like pluck() work on object properties as well as array keys.

Can I create an empty Collection? collect() with no argument (or collect([])) returns an empty Collection. You can push items in later with ->push() or ->put().

Does collect() load the array into memory all at once? Yes. Unlike lazy collections (LazyCollection), a standard Collection holds all items in memory. For very large datasets, consider LazyCollection::make() instead.