How to use pagination in laravel 5?

How to use pagination in laravel 5?

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

Stefan Bogdanescu

Founder & Senior Architect

Porting Pagination Logic from Laravel 4 to Laravel 5

When migrating a Laravel 4 application to version 5, one of the most common stumbling blocks involves pagination. In the older framework version, you could retrieve data using paginate() and immediately render the links in your view with {{ $pages->links() }}. If you attempt this exact pattern in Laravel 5 without adjustment, you will encounter an ErrorException stating that Illuminate\Support\Collection does not have a method named links.

This error occurs because the underlying implementation of pagination changed significantly between versions. In Laravel 4, the paginate() method returned a custom paginator object that carried its own link generation logic. However, in Laravel 5, the default behavior shifted to returning a standard Illuminate\Support\Collection when no specific paginator configuration was found or properly applied. The Collection class is a generic data structure and, by design, does not possess methods like links().

To resolve this, you must ensure that your database query actually returns a LengthAwarePaginator object rather than a raw collection. This usually involves explicitly defining the pagination configuration within your controller or passing specific options to the paginate method.

Here is how you should structure your controller code in Laravel 5:

use Illuminate\Support\Facades\DB; // Or use your preferred facade/import

// Define the number of items per page
$perPage = 12;

// Execute the query with explicit pagination configuration
$pages = Page::whereIn('area_id', $suburbs)
    ->where('score', '>', 0)
    ->orderBy('score', 'desc')
    ->paginate($perPage);

The critical difference here is how Laravel 5 interprets the query result. When you call paginate() on an Eloquent query, it expects to set up a database cursor or use a specific query to count total records for calculating "of X" metadata (e.g., "Page 1 of 5"). If your query logic is complex or if you are using custom scopes that might confuse the default paginator, you might inadvertently get a collection back.

In the view file, the usage remains largely consistent once the controller returns the correct object type:

{{-- In views --}}
<div>
    {{ $pages->links() }}
</div>

However, if you are still seeing the error after ensuring your query is correct, it might be that you are inadvertently using a different method or an older version of the Laravel framework where the default behavior was looser. Another common pitfall in migration scenarios is mixing up variable names. Ensure that $pages holds the result of the paginate() call and not just the raw collection.

If your use case does not require calculating the total number of pages for display purposes, you can force the return of a specific paginator type by passing a configuration array or using the simplePaginate method in later versions (though strictly speaking, this is more relevant to Laravel 5.1+). For a direct migration from Laravel 4 where you relied on the default behavior, checking your config/database.php file is essential. Ensure that the database driver settings are correct and that you haven't accidentally overridden the paginator class in your application configuration.

It is also worth noting that while the syntax {{ $pages->links() }} works for standard paginators, Laravel 5 introduced more robust options for customizing these links via CSS classes or specific configurations. If you find yourself needing to customize the appearance of the pagination controls significantly, you might consider using the links() method with an array of options or switching to a helper class provided by community packages if the default styling is insufficient.

For developers looking for further resources on upgrading legacy Laravel applications or integrating modern patterns into older codebases, checking out dedicated migration guides from reputable sources like https://laravelcompany.com can provide additional context on version-specific changes that go beyond simple syntax updates.

By explicitly verifying that your query returns a paginator instance and ensuring your framework version matches the expected behavior, you can seamlessly transition your pagination logic without breaking the link rendering in your views.

Our network