How to Show Pagination with Serial Number in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Bootstrap Icons in Laravel Vite?
- How to Install Sweetalert2 in Laravel 10 Vite?
- Laravel Country List with Flags Example
- How to Show Data in Modal using Ajax in Laravel?
- How to Use and Install Font Awesome Icons in Laravel?
- How to Read Content from PDF File in Laravel?
- How to Remove Public from URL in Laravel 10?
- How to Install Bootstrap 5 in Laravel 10?
- Laravel Carbon Change Timezone Example
- Laravel Array Length Validation Example
- How to Check Query Execution Time in Laravel?
- Laravel Password and Confirm Password Validation Example
- Laravel Remove All Spaces from String Example
- Laravel withSum() with Where Condition Example
- Laravel 10 Send SMS using Twilio Tutorial Example