Method orderBy does not exist in Laravel Eloquent?

Method orderBy does not exist in Laravel Eloquent?

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

Stefan Bogdanescu

Founder & Senior Architect

Chaining Query Builders: Why orderBy on a Collection Fails in Laravel

If you are attempting to chain an orderBy() call onto a variable that holds a collection, such as $products, and receive a BadMethodCallException, the root cause is almost certainly a misunderstanding of how Eloquent query building differs from iterating over results. The error message explicitly states that the method does not exist on the object instance you are currently manipulating. This happens because you have already executed the query.

In your specific snippet, the line $products = Product::all() performs an immediate database retrieval. At this exact moment, Eloquent transforms the relationship between your Model and the Query Builder into a standard Laravel Collection. The collection contains the actual Product model instances. While collections offer powerful utility methods for filtering arrays—like where(), map(), or filter()—they do not support query modifiers like orderBy(), skip(), or take(). These specific methods are exclusive to the Query Builder interface, which represents a pending SQL statement, not a set of loaded data.

Attempting to call ->orderBy('created_at', 'desc') on a collection fails because the database connection is already closed; the data has been hydrated into PHP objects. The collection treats orderBy as an invalid method and throws the exception you observed. Even if you were to successfully order the collection in memory using standard array sorting functions, it would be inefficient for large datasets and completely bypasses the optimization capabilities of the database engine.

To resolve this, you must ensure that the variable $products always references a Query Builder instance until the final get() call is made. This means avoiding premature execution with methods like all(), first(), or accessing properties that trigger hydration. Instead, store the initial query definition in a variable and chain your subsequent modifiers to it.

Here is the corrected implementation using a dedicated variable for the query builder:

$productsQuery = Product::query(); // Optional: explicit ::query() makes intent clear, though Product::all() would break here immediately if not handled carefully

if ($search_value) {
    $productsQuery->where('name', 'LIKE', "%{$search_value}%");
}

// Critical: Ensure we are modifying the Query Builder, not a Collection
$products = $productsQuery
    ->orderBy('created_at', 'desc')
    ->skip(10)
    ->take(10)
    ->with('tags')
    ->get();

A common pitfall in this scenario is mixing eager loading with pagination. In your original code, you called ->with('tags') before ->get(). This is correct syntax for eager loading and will work perfectly fine within the Query Builder chain. However, if you had accidentally loaded the data first, eager loading relationships on a collection is possible but does not apply to SQL-level optimizations like pagination or ordering.

If you strictly need to separate concerns, such as in a test case where you might want to inspect the query without executing it, you can still use the getRawQuery() method available on the Query Builder. Once you call ->get(), that raw SQL is gone, and you are left with data.

There are scenarios where you might see code that looks like $products = Product::all() followed by other operations. This usually indicates a misunderstanding of the Eloquent lifecycle or an attempt to refactor logic that requires keeping the query state alive. If you find yourself needing to sort a collection retrieved via ->get(), you must resort to PHP's built-in usort() function with a custom closure, accepting that this happens after the heavy lifting is done by the database.

Understanding the distinction between the Query Builder and the Collection is fundamental to writing performant Laravel applications. The Query Builder represents a request for data; the Collection represents the result of that request. You can modify the request freely, but once you fetch the results, you are limited to array manipulation methods. By keeping your variables pointing to the Query Builder until the very last moment, you maintain full access to Eloquent's powerful capabilities.

Our network