Laravel 10 Pagination Example
Tags: laravel
Published: 2023-05-09 14:15:03

Do you need an instance of Laravel 10 pagination illustrated with Blade? If you have doubts regarding Laravel 10 pagination along with the user table, I can provide you with an uncomplicated solution and example. This post will comprehensively guide you through Laravel 10 pagination. Let’s delve deeper into the topic of pagination within Laravel 10.

Pagination is deemed essential in every project regardless of its nature. Therefore, if you are new to Laravel, it is crucial to acquaint yourself with the basics of how to utilize pagination in Laravel 10, along with other functions that can be employed alongside it.
I will guide you through the process of using Laravel pagination from the very beginning in this instance. To create a rudimentary pagination example with Laravel 10, please refer to the tutorial provided below.

## Step 1. Create Route
Initially, we create a singular route for listing users with pagination. Adding both routes to your route file is a straightforward process.
**routes/web.php**

`<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BlogController;
Route::get('blog', [BlogController::class, 'index']);

?>`

**## Step 2. Create Controller**
We will be incorporating a new method for route that is similar to the ones mentioned earlier. Specifically, we will include the “index()” method which will furnish user data along with pagination details. This can be achieved by adding the following code snippet below:
**app/Http/Controllers/BlogController.php**
`<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\BlogModel;

class UserController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function index()

    {

        $data['list'] = BlogModel::paginate(10);

        return view('blog',$data);

    }

}`

**Step 3. Create Blade File**
To generate pagination automatically, create a blade file for blog in this step and include the code with links(). Simply add the following code to accomplish this task.
**resources/views/blog.blade.php**
`<!DOCTYPE html>
<html>
    <head>
        <title>Laravel 10 Pagination</title>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
    </head>
    <body>
        <div class="container">
            <h1>Laravel 10 Pagination</h1>

            <table class="table table-bordered">

                <thead>

                    <tr>

                        <th>Post Title</th>
                        <th>Description</th>
                        <th width="300px;">Action</th>

                    </tr>

                </thead>

                <tbody>

                    @if(!empty($list) && $data->count())

                    @foreach($list as $key => $value)

                    <tr>

                        <td>{{ $value->title }}</td>
                        <td>{{ $value->description }}</td>
                        <td>
                            <button class="btn btn-warning">Edit</button>
                        </td>

                    </tr>

                    @endforeach

                    @else

                    <tr>

                        <td colspan="10">There are no data.</td>

                    </tr>
                    @endif
                </tbody>
            </table>

            {!! $data->links() !!}
        </div>
    </body>
</html>`

It is possible to execute and verify this particular instance, which happens to be an uncomplicated and rudimentary demonstration.

**Note-** In case you utilize bootstrap, it is necessary to incorporate useBootstrap() onto the service provider as illustrated below:
**app\Providers\AppServiceProvider.php**
`<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;

class AppServiceProvider extends ServiceProvider
{

    /**

     * Register any application services.

     *

     * @return void

     */

    public function register()

    {

    

    }

    /**

     * Bootstrap any application services.

     *

     * @return void

     */

    public function boot()

    {

        Paginator::useBootstrap();

    }

}`

[Read More on Laravel read](readytocode.net)