Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

BadMethodCallException when orderBy() or find() is called on an Eloquent result

ErrorBadMethodCallException: Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.

Short answer

orderBy belongs to the query builder, not to a collection. After get() or all() has run, or after a relationship is read as a property, you hold a collection, and sorting there is sortBy. Move orderBy before get(), or call it on the relationship method. find exists on Eloquent collections only, not on base ones.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the class name in the message. Illuminate\Database\Eloquent\Collection is what get() and all() return. Illuminate\Support\Collection is a base collection. Then look for the call chain that puts get() or all() before the failing method.

    grep -rnE "(get|all)\(\)->(orderBy|find)\(" app/
  2. 2Match what you see, then jump to its fix

    1. 1Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.get() or all() ran before orderBy()Fix
    2. 2The failing line looks like $user->posts->orderBy('created_at')A relationship is read as a property and then orderedFix
    3. 3Method Illuminate\Support\Collection::find does not exist.find() is called on a base collection, which does not have itFix

What does the error look like?

  • BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
  • BadMethodCallException: Method Illuminate\Support\Collection::find does not exist.

Cause 1 of 3

get() or all() ran before orderBy()

1Check

Eloquent models act as query builders, so orderBy() works while you are still building the query. get() runs the query and returns an Illuminate\Database\Eloquent\Collection, which has no orderBy method.

// in php artisan tinker
>>> get_class(App\Models\Post::where('active', 1));
>>> get_class(App\Models\Post::where('active', 1)->get());

2Fix

Put orderBy() before get() to sort in the database. If the rows are already loaded, sort the collection with sortBy() or sortByDesc(). A sorted collection keeps its old keys, so call values() to number them again.

// fails: get() already returned a collection
$posts = Post::where('active', 1)->get()->orderBy('title');

// sort in the database
$posts = Post::where('active', 1)->orderBy('title')->get();

// sort rows that are already in memory
$posts = Post::where('active', 1)->get()->sortBy('title')->values();

3Verify

The query returns rows in the order you asked for.

// in php artisan tinker
>>> App\Models\Post::where('active', 1)->orderBy('title')->pluck('title');

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

A relationship is read as a property and then ordered

1Check

A relationship read as a property is lazy loaded, and what you get is the collection of related models. The same relationship called as a method, with parentheses, is a query builder.

grep -rnE "\->[a-z_]+->orderBy\(" app/ resources/views/

2Fix

Call the relationship method and chain orderBy() before get(). To give every use of the relationship one default order, put orderBy() inside the relationship method.

// fails: ->posts is already a collection
$posts = $user->posts->orderBy('created_at');

// query the relationship
$posts = $user->posts()->orderBy('created_at')->get();

// default order inside the model
public function posts()
{
    return $this->hasMany(Post::class)->orderBy('created_at');
}

3Verify

Load the page that shows the list. The related rows come in order and no exception is raised.

// in php artisan tinker
>>> App\Models\User::first()->posts()->orderBy('created_at')->pluck('created_at');

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

find() is called on a base collection, which does not have it

1Check

find() is one of the extra methods of Eloquent collections. Base collections, from collect(), from pluck() and from map() that no longer return models, do not have it. The Eloquent collection turns into a base collection after collapse, flatten, flip, keys, pluck and zip.

// in php artisan tinker
>>> get_class(App\Models\Post::all());
>>> get_class(App\Models\Post::all()->pluck('title'));

2Fix

On a base collection, look the item up with firstWhere() or where()->first(). If you still need models, keep the Eloquent collection, or fetch the row directly with Post::find().

// base collection of arrays or plain values
$post = $items->firstWhere('id', 5);

// a collection of models: find() works
$posts = Post::all();
$post = $posts->find(5);

// a single row from the database
$post = Post::find(5);

3Verify

The call returns the model or the array row, not an exception.

// in php artisan tinker
>>> App\Models\Post::all()->find(1)?->title;

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Decide where each sort belongs. Large lists should be ordered by the database before get(). sortBy() suits small lists that are already in memory.
  • Name variables by what they hold, such as $postQuery and $posts, so a query is not mistaken for a result.
  • After pluck(), map() or collapse(), remember you hold a base collection and use its methods only.
  • Read the class name in the exception message first. It says which kind of object you are holding.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.