Home > Laravel > How to Make Pagination in Laravel 8 – Example Tutorial?

How to Make Pagination in Laravel 8 – Example Tutorial?

So in this tutorial, We will learn about how to make pagination in laravel 8 (latest version) with help of some examples.

Making Pagination in laravel is very easy. It is a built-in feature and you can easily implement it in your application.

Laravel’s paginator is integrated with the query builder and Eloquent ORM, and easy-to-use pagination of database record with zero configuration.

The default paginator generates the HTML markup compatible with the Tailwind CSS framework. But if you want to make pagination compatible with the Bootstrap framework then you can also do that.

We will cover creating a laravel app, setting up routes, controllers, and models but the main focus will be on the pagination using the paginate() function. We will see it from scratch to end.

Let’s get started to make pagination in the laravel 8 application without doing many configurations.

Create a Laravel Application

Let’s first start with creating laravel application. First, we will run the composer command to create the laravel project with default files and folder structure.

If you don’t have composer installed on your machine then you can check the composer installation guide here. I also explained in this guide how to create laravel applications in detail.

Create Model and Migration

After creating the project, now create the Model and Migration for your laravel project because we need a database table to store the records. So run the following command to create the Model and Migration together.

The above command will make the Student model and create a migration automatically using the -m flag. It will create the migration name from the helping name convention of the model name.

You can check the migration file by the following path database/migrations/2021_12_26_000000_create_students_table.php. And you will see in this file has a table schema and you can add or delete the columns here.

If you want to check your model file the go to this path app/Models/Student.php. Don’t forget to register the schema in the $fillable array, if you have added new columns in the migration file.

Run Migration

After editing the migration and model file, use the following command to run the migration files and it will create the tables in your database.

Now you can see the database table and also insert some dummy data to show on the frontend and make pagination with laravel’s paginate() function.

Create Controller

Run the following command to create the Student controller. In the controller, you can handle all the requests and logic.

See the controller by following this path app/Http/StudentController.php and make a simple index() function to fetch the all students records.

Create Route

Create routes in the routes/web.php file and add the following code in this file to make routes for StudentController.

Create Blade View

Now create a new blade file resources/views/student.blade.php and make the HTML structure to show the students records. To show the student records in view use the following reference code.

Now we will implement laravel pagination functionality on these student records and them presentable by setting the record show limit.

Make Laravel Pagination

Let’s now make pagination in laravel to show the Student’s data with the limit and then paginate the data to show more Student’s data.

Laravel pagination is very easy and you can simply integrate with the query builder and Eloquent ORM in the controller.

To integrate pagination in laravel, you just have to use the paginate() function instead all() function in index() function of StudentController. The paginate() function takes an argument that is the number of results you want to show.

You can also filter the student data using the orderBy(), where() condition and many more together. See the following index() function has implemented paginate() functionality.

To display the paginate links in view, you need to use the links() function with the $students variable. So add the following reference code in the student.blade.php file.

After adding the above code to your file, you will see the pagination links at the bottom of your result data. See the reference image below.

laravel pagination links

And your blade view code like the following example code. You can make the design structure as you want.

More Ways to Use Laravel Pagination

There are more ways to use pagination in laravel and also you can customize the pagination result according to some additional parameters.

So let’s see the other useful methods of laravel pagination and how we can customize the pagination.

Laravel Simple Pagination

If you only want to display simple “Next” and “Previous” links in your application, then you can use the simplePaginate() function. See the following example code.

Laravel Cursor Pagination

In the cursor pagination, it takes a “cursor” string in the URL query string. The cursor is an encoded string containing the location that the next paginated query should start paginating and the direction that it should paginate.

So we will use the cursorPaginate() function to make the cursor pagination in laravel. See the following code as an example.

Sample URL of cursor pagination

Customizing Pagination URLs

By default, laravel pagination links work on the current request URI. But if you want to change the URL with a custom path then you can do it with the withPath() function of the paginator.

Append Query String Values

If you want to append the query string in the pagination links then you may use the appends() method. It takes the array with key pair values.

You can also use the same method in view, where you are showing the pagination links.

Append Hash Fragments

If you want to append a “hash fragment” to URLs generated by the paginator, you may use the fragment() method. This method will put the hash string (#students) to the end of each pagination link.

Converting Results To JSON

The Laravel paginator classes implement the Illuminate\Contracts\Support\Jsonable Interface contract and expose the toJson method.

So it is very easy to convert pagination results into JSON. You may also convert a paginator instance to JSON by returning it from a route or controller action.

In Controller file
In routes/web.php file

Using Bootstrap in Pagination UI

By default, the UI of pagination links is compatible with the Tailwind CSS framework. But if you want to use the bootstrap framework for pagination UI then you can do it by using the useBootstrap() function in the boot() method of AppServiceProvider.php file. And use the namespace of pagination in this file at top.

You can find this file by following this path app/Providers/AppServiceProvider.php.

Paginator Instance Methods

Each paginator instance provides additional pagination information via the following methods:

Method Description
$paginator->count() Get the number of items for the current page.
$paginator->total() Determine the total number of matching items in the data store.
$paginator->currentPage() Get the current page number.
$paginator->firstItem() Get the result number of the first item in the results.
$paginator->getOptions() Get the paginator options.
$paginator->hasPages() Determine if there are enough items to split into multiple pages.
$paginator->hasMorePages() Determine if there are more items in the data store.
$paginator->items() Get the items for the current page.
$paginator->lastItem() Get the result number of the last item in the results.
$paginator->lastPage() Get the page number of the last available page.
$paginator->onFirstPage() Determine if the paginator is on the first page.
$paginator->perPage() The number of items to be shown per page.
$paginator->nextPageUrl() Get the URL for the next page.
$paginator->previousPageUrl() Get the URL for the previous page.
$paginator->getPageName() Get the query string variable used to store the page.

So these are the main paginator instance methods that you can use to make any customization or functionality to display the pagination results. More methods can see on the official laravel website.

Conclusion

So, in this tutorial, you learned about how to create laravel application and how pagination works and can implement it on your laravel application.

You also have learned about the laravel pagination customization like append query string, append hash fragment, simple and cursor pagination, convert pagination result to a JSON object, and using bootstrap for pagination UI.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment