Navigation

Laravel

What is the Difference Between `->get()` and `->all()`?

Understand Laravel Eloquent's get() vs all() methods in 2025. Learn performance differences, when to use each, and avoid common collection vs query builder mistakes.

Table Of Contents

Laravel's Built-in Solution: Two Different Approaches

The difference is simple: get() executes a query builder chain, while all() fetches everything from the table immediately:

// get() - Executes the query builder chain
$activeUsers = User::where('active', true)
                   ->orderBy('created_at', 'desc')
                   ->get(); // Returns Collection

// all() - Ignores query builder, fetches ALL records
$allUsers = User::where('active', true)  // This is ignored!
                ->orderBy('created_at')  // This too!
                ->all(); // Returns ALL users from table

// Correct usage of all()
$allUsers = User::all(); // Gets every user in the table

// get() with specific columns
$users = User::select('id', 'name', 'email')
             ->where('active', true)
             ->get();

// all() with specific columns
$allUserNames = User::all(['id', 'name']); // Gets ALL users, only id and name

Performance and Memory Considerations

Never use all() on large tables - it loads everything into memory. The get() method respects your query constraints and is generally safer:

// DANGEROUS on large tables
$users = User::all(); // Could load 100,000+ records

// BETTER - use pagination or limits
$users = User::take(50)->get();
$users = User::paginate(15);
$users = User::where('created_at', '>', now()->subDays(30))->get();

// Common mistake - all() ignores constraints
$recentUsers = User::where('created_at', '>', now()->subWeek())
                   ->all(); // Still gets ALL users!

// Correct approach
$recentUsers = User::where('created_at', '>', now()->subWeek())
                   ->get(); // Gets only recent users

Advanced usage patterns:

// Chunking for large datasets (better than all())
User::chunk(100, function ($users) {
    foreach ($users as $user) {
        // Process each user
    }
});

// Lazy collections for memory efficiency
User::where('active', true)->lazy()->each(function ($user) {
    // Process one by one without loading all into memory
});

// exists() vs get()->count() vs all()->count()
$hasUsers = User::where('active', true)->exists();        // Most efficient
$userCount = User::where('active', true)->count();        // Good
$userCount = User::where('active', true)->get()->count(); // Less efficient
$userCount = User::all()->where('active', true)->count(); // Terrible - loads everything!

// Collection methods after get()
$users = User::where('verified', true)->get();
$groupedUsers = $users->groupBy('role');
$userEmails = $users->pluck('email');

// Remember: all() returns Collection, get() returns Collection
$collection1 = User::all();        // Collection instance
$collection2 = User::get();        // Collection instance  
$query = User::where('active', 1); // Builder instance

Key rule: Use get() for filtered results, all() only when you literally need every record in the table. The all() method ignores any query builder methods you've chained before it, which often leads to "why is my where clause not working?" confusion.

Related: Laravel Collections: Beyond Basic Array Operations | Advanced Eloquent Techniques and Optimizations in Laravel | Building Multi-tenant Applications with Laravel: A Comprehensive Guide

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Laravel