Order by row and limit result in Laravel 5

Order by row and limit result in Laravel 5

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

Stefan Bogdanescu

Founder & Senior Architect

Order by row and limit result in Laravel 5

The error message Call to undefined method Illuminate\Database\Eloquent\Collection::orderBy() is a classic symptom of applying eager loading or query chaining incorrectly within Laravel's ecosystem. It occurs because you are attempting to call a database query method on an object that has already been executed against the database, resulting in a fully hydrated Collection rather than a Query Builder instance.

In your specific case, the line $full = Fulls::all() is the culprit. The all() method executes the query immediately and retrieves every record matching the scope. Once Laravel fetches this data from the database, it returns an instance of Illuminate\Database\Eloquent\Collection. This collection represents a set of model instances in memory; it does not know how to interact with the database schema directly. Consequently, when you chain .orderBy('count', 'desc') onto this Collection object, PHP throws a fatal error because that method simply does not exist on the Collection class.

To achieve the goal of selecting the top 5 results ordered by the count column in descending order, you must construct your query using the Query Builder before execution happens. The correct approach involves chaining methods to build the query logic first, then executing it only at the very end.

Here is the corrected implementation:

$full = Fulls::orderBy('count', 'desc')->take(5)->get();

By removing the all() call, you ensure that you are still working with a Query\Builder object. In this state, the application knows you intend to run a database query, so it accepts methods like orderBy(), take(), and get() in the order they are chained. The flow of execution remains:

  1. Fulls::: Instantiates the Query Builder for the fulls table.
  2. orderBy('count', 'desc'): Appends a SQL ORDER BY count DESC clause to the generated query string.
  3. take(5): Appends a LIMIT 5 clause, restricting the result set size.
  4. get(): Executes the final SQL statement and returns the collection of results.

This pattern is fundamental to Laravel's "Query Builder" philosophy. While all() is useful when you need every single row in your table regardless of ordering or limits, mixing it with pagination or sorting logic defeats the purpose of lazy evaluation. If you execute the query too early, you lose the ability to refine the results without fetching unnecessary data into memory first.

If you are working within a controller or a service class where this logic is repeated frequently, consider utilizing an Eloquent Scope to encapsulate this ordering behavior. You can define a scope in your Full model:

public function scopeTopCounts($query)
{
    return $query->orderBy('count', 'desc')->take(5);
}

With this scope defined, your usage becomes cleaner:

$full = Fulls::topCounts()->get();

This abstraction improves readability and ensures consistency across your codebase. It also prevents accidental execution of all() elsewhere in the application that might lead to performance bottlenecks or the specific error you encountered. Always remember that in Laravel, unless you explicitly execute a query via methods like find(), first(), or get(), the framework does not touch the database.

Our network