How to Show Pagination with Serial Number in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hello Folks,

I am going to explain to you example of laravel pagination serial number. In this article, we will implement a auto increment serial number in laravel. I explained simply about how to display serial number in laravel. Here you will learn laravel create serial number.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version as well.

In this example, i will give you three example to generate auto increment serial number in laravel. we will use firstItem(), iteration and key to show serial number in laravel application.

Let's see one by one example:

Example 1: Laravel Pagination with Serial Number

First, we will add route as like the below to example:

routes/web.php

Route::get('users', [UserController::class, 'index']);

Next, we need to create new UserController with index method as like the below:

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$users = User::paginate(10);

return view('users', compact('users'));

}

}

Next, we need to create new users.blade.php with serial number logic:

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Auto Increment Serial Number Example - ItSolutionStuff.com</title>

<meta name="csrf-token" content="{{ csrf_token() }}">

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h1>Laravel Auto Increment Serial Number Example - ItSolutionStuff.com</h1>

<table class="table table-bordered data-table" >

<thead>

<tr>

<th>No</th>

<th>Name</th>

<th>Email</th>

</tr>

</thead>

<tbody>

@foreach($users as $key => $user)

<tr>

<td>{{ $users->firstItem() + $key }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>

</tr>

@endforeach

</tbody>

</table>

{{ $users->links() }}

</div>

</body>

</html>

Output:

Example 2

Now, we need to create new users.blade.php with serial number logic:

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Auto Increment Serial Number Example - ItSolutionStuff.com</title>

<meta name="csrf-token" content="{{ csrf_token() }}">

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h1>Laravel Auto Increment Serial Number Example - ItSolutionStuff.com</h1>

<table class="table table-bordered data-table" >

<thead>

<tr>

<th>No</th>

<th>Name</th>

<th>Email</th>

</tr>

</thead>

<tbody>

@foreach($users as $user)

<tr>

<td>{{ $loop->iteration }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>

</tr>

@endforeach

</tbody>

</table>

</div>

</body>

</html>

Output:

Example 3

Now, we need to create new users.blade.php with serial number logic:

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Auto Increment Serial Number Example - ItSolutionStuff.com</title>

<meta name="csrf-token" content="{{ csrf_token() }}">

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h1>Laravel Auto Increment Serial Number Example - ItSolutionStuff.com</h1>

<table class="table table-bordered data-table" >

<thead>

<tr>

<th>No</th>

<th>Name</th>

<th>Email</th>

</tr>

</thead>

<tbody>

@foreach($users as $key => $user)

<tr>

<td>{{ ++$key }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>

</tr>

@endforeach

</tbody>

</table>

</div>

</body>

</html>

Output:

I hope it can help you...

Tags :
Shares