How to paginate and sort results in Laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# How to Paginate and Sort Results in Laravel: The Developer's Guide As a senior developer working with Laravel, you constantly run into scenarios where you need to present data efficiently—specifically, handling both pagination (breaking large datasets into manageable pages) and sorting (ordering the results). The common struggle, as highlighted in your question, is knowing how to combine these operations without breaking the pagination links that are crucial for user navigation. Let's dive into why your initial attempts failed and establish the correct, idiomatic way to handle sorting and pagination in Laravel using Eloquent. ### Understanding the Pitfall: Paginator vs. Query The fundamental issue lies in what you are manipulating: the result set versus the paginator object itself. When you call `$model->paginate(10)`, Laravel returns a `LengthAwarePaginator` object. This object holds all the necessary links, total counts, and the data for the current page. If you try to call methods like `sortBy()` directly on this object, you are modifying the *presentation layer*, not the underlying database query that generated those links. Consequently, the pagination structure remains intact, but the sorting logic is either ignored or applied incorrectly to the subset of data presented to the user. ### The Correct Approach: Sorting Before Pagination The correct practice in Laravel is to apply all necessary filtering and ordering constraints directly to the Eloquent Query Builder *before* you invoke the pagination method. This ensures that the pagination links are generated based on the correctly sorted result set. #### Method 1: Applying Sort via `orderBy()` (The Best Practice) To sort your data, use the `orderBy()` method on your Eloquent query. This tells the database exactly how to order the records before Laravel handles the limit and offset for pagination. Consider this scenario where we want to paginate results by page and sort them alphabetically by the `name` column: ```php use App\Models\YourModel; // 1. Define the sorting criteria $sortBy = 'name'; // 2. Apply sorting before pagination $paginated = YourModel::orderBy($sortBy) ->paginate(10); // 3. Pass the paginator to your controller/view return $paginated; ``` In your controller, when you return this result (typically as a JSON response), Laravel automatically handles structuring the pagination links correctly alongside the sorted data. This approach keeps the separation clean: the query defines *what* data is fetched and ordered, and `paginate()` handles *how* that data is split into pages. This aligns perfectly with best practices for building robust APIs in Laravel. ### Integrating Resources for Transformation Once you have the properly sorted and paginated collection, integrating your Resource is straightforward. The Resource is designed to transform a single model or a collection of models into a specific structure, which is exactly what we want for API responses. ```php // Assuming $paginated is the result from Method 1 above $results = \App\Http\Resources\YourModelResource::collection($paginated); return $results; ``` Notice that we do not need to call `sortBy()` on the `$results` collection here, because the sorting was established at the database level in the previous step. The Resource simply iterates over the already ordered data and formats it for the API response. This separation of concerns—Query Builder for logic, Paginator for structure, and Resources for presentation—is a hallmark of well-architected Laravel applications. ### Summary and Conclusion To successfully paginate and sort results in Laravel without losing pagination links, remember this golden rule: **Always apply sorting using Eloquent’s query methods (`orderBy()`) before calling pagination methods (`paginate()`).** By manipulating the underlying query, you ensure that the pagination mechanism operates on the correctly ordered dataset. This strategy is efficient, database-friendly, and results in clean, predictable API responses. Mastering this interaction between Eloquent queries, pagination objects, and Resources is key to writing powerful and maintainable code within the Laravel ecosystem.