How to display pagination in laravel?

by elisha_langworth , in category: PHP CMS , 8 months ago

How to display pagination in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by gilbert , 8 months ago

@elisha_langworth 

To display pagination in Laravel, you need to follow these steps:

  1. In your controller method where you fetch the data that you want to paginate, add the following code to paginate the query results:
1
$posts = Post::paginate(10);


In this example, Post is the model for your posts and paginate(10) will paginate the results with 10 records per page.

  1. In your blade view file where you want to display the paginated data, you can use the links method on the paginated data variable to display pagination links. Here's an example:
1
2
3
4
5
@foreach ($posts as $post)
    <!-- Display post data here -->
@endforeach

{{ $posts->links() }}


This code will display the paginated posts and the pagination links at the bottom of the page. Laravel will generate the necessary HTML code for the pagination links.


That's it! You have successfully displayed pagination in Laravel.