Laravel 10 PHP Framework Pagination Example

Laravel 10 PHP Framework Pagination Example

Pagination is an essential feature of any web application that displays large amounts of data. Laravel provides a simple and efficient way to implement pagination in your application using the built-in Paginator class. In this tutorial, we will walk you through how to use pagination in Laravel 10 PHP Framework.

First, let’s create a sample database table to work with. We’ll use the following schema to create a “users” table:

  name VARCHAR(255),
  email VARCHAR(255),

Once the table is created, we’ll insert some sample data into the table:

INSERT INTO users (name, email) VALUES
('John Doe', '[email protected]'),
('Jane Doe', '[email protected]'),
('Bob Smith', '[email protected]'),
('Alice Jones', '[email protected]'),
('Mike Johnson', '[email protected]'),
('Sarah Wilson', '[email protected]'),
('Tom Davis', '[email protected]'),
('Amy Lee', '[email protected]'),
('David Brown', '[email protected]'),
('Mary Jackson', '[email protected]');

With our sample data in place, we can now create a Laravel 10 PHP Framework application and implement pagination. Here are the steps to do so:

Step 1: Install Laravel 10 PHP Framework If you haven’t already installed Laravel, you can do so by running the following command:

composer create-project --prefer-dist laravel/laravel myapp

This will create a new Laravel 10 PHP Framework application in a directory called “myapp”.

Step 2: Create a Controller Next, we’ll create a controller to handle the pagination logic. Run the following command to create a new controller:

php artisan make:controller UserController

This will create a new UserController.php file in the app/Http/Controllers directory.

Step 3: Define the index() method Open the UserController.php file and add the following code to define the index() method:

use Illuminate\Support\Facades\DB;

public function index()
    $users = DB::table('users')->paginate(5);

    return view('users', ['users' => $users]);

In the above code, we’re using the DB facade to fetch the data from the “users” table and then paginating the results using the paginate() method with a limit of 5 records per page. Finally, we’re passing the paginated data to a view called “users”.

Step 4: Create a View Next, we’ll create the view that will display the paginated data. Create a new file called “users.blade.php” in the resources/views directory and add the following code:

<!DOCTYPE html>
    <title>Laravel 10 Pagination Example</title>
            <th>Created At</th>
            <th>Updated At</th>
        @foreach ($users as $user)
                <td>{{ $user->id }}</td>
                <td>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>
                <td>{{ $user->created_at }}</td>
                <td>{{ $user->updated_at }}</td>

    {{ $users->links() }}

In the above code, we’re using the Blade templating engine to loop through the paginated data and display it in an HTML table. We’re also using the links() method to generate the pagination links.

Step 5: Test the Application We’re now ready to test our application. Start the Laravel development server by running the following command:

php artisan serve

This will start the development server at http://localhost:8000. Navigate to this URL in your browser, and you should see the paginated data displayed on the screen.

Conclusion In this tutorial, we’ve shown you how to use pagination in Laravel 10 PHP Framework. By using the built-in Paginator class, we can easily paginate large amounts of data and display it to the user in a user-friendly manner.

This Article Ideas has been taken from the following websites; which are following:


Leave a Reply

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