ItSolutionStuff.com

How to Show Pagination with Serial Number in Laravel?

By Hardik Savani • April 16, 2024
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Bootstrap Icons in Laravel Vite?

Read Now →

How to Install Sweetalert2 in Laravel 10 Vite?

Read Now →

Laravel Country List with Flags Example

Read Now →

How to Show Data in Modal using Ajax in Laravel?

Read Now →

How to Use and Install Font Awesome Icons in Laravel?

Read Now →

How to Read Content from PDF File in Laravel?

Read Now →

How to Remove Public from URL in Laravel 10?

Read Now →

How to Install Bootstrap 5 in Laravel 10?

Read Now →

Laravel Carbon Change Timezone Example

Read Now →

Laravel Array Length Validation Example

Read Now →

How to Check Query Execution Time in Laravel?

Read Now →

Laravel Password and Confirm Password Validation Example

Read Now →

Laravel Remove All Spaces from String Example

Read Now →

Laravel withSum() with Where Condition Example

Read Now →

Laravel 10 Send SMS using Twilio Tutorial Example

Read Now →