Model::all

Retrieves every row from the model's table as an Eloquent Collection of hydrated models.

Signature
public static function all(array|string $columns = ['*']): \Illuminate\Database\Eloquent\Collection

Parameters

ParameterTypeDescription
$columnsoptionalarray|string= ['*']Columns to select. Accepts an array, or column names as separate variadic arguments, which are collected into an array.

Return value

\Illuminate\Database\Eloquent\CollectionAn Illuminate\Database\Eloquent\Collection containing one hydrated model per row. The collection is empty, never null, when the table has no rows.

Errors

Throws Illuminate\Database\QueryException if the underlying table or a selected column does not exist.

all() is the shortest path from a model class to its rows. It is a static method that opens a fresh query, applies the model's global scopes, runs select * from table, and hydrates each row into a model instance inside an Illuminate\Database\Eloquent\Collection.

When to reach for it

Use it for bounded reference data — currencies, countries, roles, plan tiers — where you genuinely want the whole table and know it will stay small. For anything user-generated, reach for constrained queries and paginate() instead.

What trips people up

The static call always starts a brand new query. It is not a terminator you can chain onto a builder: Flight::where('active', 1)->all() does not filter — Eloquent's builder has no all method at all, so the call falls through __call and raises a BadMethodCallException. The method you want at the end of a chain is get().

The other trap is memory. Every row becomes a full PHP object with its attribute array, original array, and casts. A table that is comfortable in the database can exhaust memory_limit once hydrated. When you only need to iterate, chunk(), lazy(), or cursor() keep the footprint flat.

Selecting a narrow column list makes the models partial — attributes you did not select are simply absent, and touching one returns null rather than erroring.

Example

Example · php
<?php

use App\Models\Flight;

// Every row, hydrated into Flight models.
$flights = Flight::all();

echo $flights->count(), " flights\n";

foreach ($flights as $flight) {
    echo $flight->name, "\n";
}

// Select a narrow column list to keep the models light.
$names = Flight::all(['id', 'name']);

print_r($names->pluck('name', 'id')->all());

When to use it

  • Load a small lookup table such as countries or plan tiers to build a select box.
  • Seed an in-memory cache of role records at application boot.
  • Export a compact configuration table to CSV in an Artisan command.

Frequently asked questions

What is the difference between Model::all() and Model::get()?

all() is a static method that opens a fresh, unconstrained query, while get() runs whatever query you have already built. Because all() discards prior chaining, Flight::where('active', 1)->all() does not filter — and in fact throws BadMethodCallException, since Eloquent's builder defines no all() method. Use all() when you want the whole table and get() at the end of any chain.

Does Model::all() return null when the table is empty?

No — it always returns an Illuminate\Database\Eloquent\Collection, which is simply empty when there are no rows. You can safely call count(), isEmpty(), or foreach over the result without a null check, and methods like first() on that empty collection return null rather than erroring.

How do I loop over a large table without running out of memory?

Replace all() with chunk(), chunkById(), lazy(), or cursor(), which keep only a slice of models in memory at a time. chunk(500, fn ($rows) => ...) processes 500 records per query, and cursor() streams one model at a time using a PHP generator. If you only need an aggregate, run sum() or count() directly so the database computes it without hydrating anything.

More examples

all() versus a constrained get()

The first count covers the whole table because all() ignores anything you chained beforehand. The second count reflects the active rows. The commented line shows the mistake that produces a BadMethodCallException rather than a filtered result.

Example · php
<?php

use App\Models\Flight;

// Starts a fresh query - the where() below is ignored entirely.
$wrong = Flight::where('active', 1);
$all   = Flight::all();          // select * from flights

echo $all->count(), "\n";        // every flight, active or not

// The correct terminator for a constrained query.
$active = Flight::where('active', 1)
    ->orderBy('name')
    ->get();

echo $active->count(), "\n";     // only the active flights

// This line throws BadMethodCallException - there is no Builder::all().
// Flight::where('active', 1)->all();

Swapping all() for a memory-safe traversal

All three lines print the same total. The first holds every invoice in memory simultaneously, the second caps it at 500 models per iteration, and the third never hydrates a model at all because the aggregate runs inside the database.

Example · php
<?php

use App\Models\Invoice;

// Hydrates every invoice at once - fine for hundreds, fatal for millions.
$total = Invoice::all()->sum('amount_cents');

echo $total, "\n";

// Same answer, constant memory: 500 models alive at a time.
$running = 0;

Invoice::chunk(500, function ($invoices) use (&$running) {
    $running += $invoices->sum('amount_cents');
});

echo $running, "\n";

// Cheapest of all - the database does the arithmetic, no models built.
echo Invoice::sum('amount_cents'), "\n";

Global scopes still apply

The first count excludes soft-deleted posts because the SoftDeletes global scope is applied to the fresh query that all() opens. The later counts show how to reach past that scope, and they use get() because withTrashed() returns a builder.

Example · php
<?php

use App\Models\Post;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
}

// SoftDeletes adds a global scope, so this is NOT a bare select *.
$posts = Post::all();
// select * from posts where posts.deleted_at is null

echo $posts->count(), "\n";

// Include the trashed rows explicitly.
echo Post::withTrashed()->get()->count(), "\n";

// Only the trashed rows.
echo Post::onlyTrashed()->get()->count(), "\n";

Discussion

  • Be the first to comment on this lesson.