Laravel paginate order by

Laravel paginate order by

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel paginate order by: Fixing the paginate() on a non-object Error

When attempting to retrieve paginated data in Laravel, developers often encounter a specific error that halts execution before any HTML is rendered. Consider a scenario where you are fetching recent news items, ordering them by creation date, and trying to pass the result to a view. The code snippet below demonstrates the common mistake that triggers the Call to a member function paginate() on a non-object exception.

$ondata = DB::table('official_news')
    ->orderBy('created_date', 'desc')
    ->get()
    ->paginate(7);
return View::make('main', ['ondata' => $ondata]);

The core issue lies in the chain of method calls. The DB::table() function returns a query builder instance, which allows for chaining methods like orderBy. However, the moment you call .get(), the query is executed immediately against the database. This action retrieves all matching records and converts them into a native collection or array. Once that data has been fetched from the database, the object returned by .get() is no longer an Eloquent query builder instance capable of further chaining. It is simply a static dataset. Consequently, attempting to call .paginate(7) on this loaded collection results in a fatal error because the Collection class does not possess a paginate method.

To resolve this, you must separate the ordering logic from the data retrieval logic. The pagination helper needs access to the query builder to calculate the total count and handle the offset correctly. If you execute .get() first, you lose that ability. The correct approach is to chain the paginate method directly after the query modifications, such as orderBy, and only then retrieve the result.

Here is the corrected implementation:

$ondata = DB::table('official_news')
    ->orderBy('created_date', 'desc')
    ->paginate(7);

return View::make('main', ['ondata' => $ondata]);

By removing .get() from the chain, you allow Laravel to handle the pagination mechanics. When paginate(7) is called on the query builder, Laravel automatically splits the result set into pages of 7 items at a time. It also calculates the total number of records and determines which page is currently being requested based on the route parameters (typically ?page=2).

It is important to understand what happens behind the scenes with this correction. When you call .paginate() without first calling .get(), Laravel returns an instance of LengthAwarePaginator. This object contains the actual data for the current page, but it also holds metadata essential for rendering pagination controls in your view, such as the total count, the number per page, and links to previous or next pages. If you had used .get() first, you would have loaded every single record into memory before attempting to paginate them, which is inefficient and incorrect.

Furthermore, remember that orderBy can accept two arguments: the column name and the direction (ascending or descending). In the corrected example, 'created_date' is ordered by 'desc', ensuring the newest news items appear first on the first page. If you omit the direction, Laravel defaults to ascending order, which might not be desired for a feed of recent updates.

Finally, ensure that your view expects an object with pagination properties rather than a raw array. When passing $ondata to View::make, the paginator provides methods like links(), total(), and each() to easily generate the pagination UI components within your Blade template without needing to manually calculate offsets or totals.

Our network