Create New Post

Laravel - Pagination Customizations

Laravel provides a convenient way to paginate database query results using the built-in pagination features. You can customize the pagination appearance, style, and behavior to suit your application's needs.

Here are some common customization options for pagination in Laravel:

1. Customizing the View

Laravel uses Blade templates to render the pagination links. You can customize the appearance by publishing the pagination views:

php artisan vendor:publish --tag=laravel-pagination

This command will publish the pagination views to the resources/views/vendor/pagination directory. You can modify the Blade files in this directory to customize the HTML and styling of the pagination links.

2. Customizing the Query Parameter

You can customize the query parameter used for pagination. By default, Laravel uses the page parameter. To change it, you can set the query method on the paginator instance:

$items = DB::table('your_table')->paginate(10);
$items->withQueryString(['your_page_parameter' => $items->currentPage()]);

3. Customizing the Number of Items Per Page

When paginating, you can specify the number of items displayed per page. The paginate method accepts the number of items you want per page:

$items = DB::table('your_table')->paginate(15);

4. Displaying Page Numbers

If you want to display the page numbers instead of "Previous" and "Next" links, you can use the onEachSide method:

$items = DB::table('your_table')->paginate(10)->onEachSide(5);

This will display page numbers on both sides of the current page.

5. Customizing the "Next" and "Previous" Text

You can customize the text for the "Next" and "Previous" links by using the setPageName method:

$items = DB::table('your_table')->paginate(10)->setPageName('custom_page');

This will use custom_page instead of page in the URL.

6. Manually Creating Pagination Links

If you need more control over the pagination links, you can use the links method to generate the HTML for the links:

{!! $items->links('custom.pagination.view') !!}

This allows you to use a custom Blade view for rendering the pagination links.


Leave a Reply

Your email address will not be published. Required fields are marked *