Lesson 29 of 50 - Learn how to display records page by page and search database records in Laravel.
Pagination divides a large number of database records into smaller pages. Instead of displaying every student on one page, Laravel can display a limited number per page.
For example:
Without pagination:
$students = DB::table('students')->get();
With pagination:
use Illuminate\Support\Facades\DB;
$students = DB::table('students')->paginate(10);
The paginate(10) method displays up to
10 records per page and calculates the total number
of matching records.
First, create a controller:
php artisan make:controller StudentController
Open app/Http/Controllers/StudentController.php
and add a method to retrieve paginated students.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function index()
{
$students = DB::table('students')
->orderBy('id', 'desc')
->paginate(10);
return view('students.index', compact('students'));
}
}
This method retrieves the latest students first and passes the paginated result to the Blade view.
Open routes/web.php and register the route.
use App\Http\Controllers\StudentController;
use Illuminate\Support\Facades\Route;
Route::get('/students', [StudentController::class, 'index'])
->name('students.index');
Open the following URL in your browser after starting the Laravel development server:
http://127.0.0.1:8000/students
Ensure your database connection is configured and the students table exists before opening this page.
Create the following file:
resources/views/students/index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Students List</title>
</head>
<body>
<h2>Students List</h2>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@forelse ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->email }}</td>
</tr>
@empty
<tr>
<td colspan="3">No students found.</td>
</tr>
@endforelse
</tbody>
</table>
<div>
{{ $students->links() }}
</div>
</body>
</html>
The links() method generates pagination
navigation links.
Laravel's default pagination views use Tailwind CSS.
If your website uses Bootstrap, configure Laravel's
pagination views for Bootstrap, for example by calling
Paginator::useBootstrapFive() in
AppServiceProvider::boot().
You can use the request query string to search students. For example:
/students?search=Rahul
Update the controller's index() method:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
public function index(Request $request)
{
$search = trim($request->query('search', ''));
$students = DB::table('students')
->when($search !== '', function ($query) use ($search) {
$query->where('name', 'like', '%' . $search . '%');
})
->orderBy('id', 'desc')
->paginate(10)
->withQueryString();
return view('students.index', compact('students', 'search'));
}
The when() method applies the search
condition only when a search term is provided.
withQueryString() keeps the search
parameters when navigating between pages.
Add this form above the students table in
resources/views/students/index.blade.php.
<h2>Search Students</h2>
<form action="{{ route('students.index') }}" method="GET">
<input
type="text"
name="search"
placeholder="Enter student name"
value="{{ $search }}"
>
<button type="submit">Search</button>
<a href="{{ route('students.index') }}">Reset</a>
</form>
When a user enters a name and submits the form, Laravel searches the matching records.
The form uses GET because searching does not modify
database records. Laravel's Blade output syntax
{{ $search }} escapes the search value.
You can add multiple search filters to the same query.
Suppose your students table has a course
column.
Controller example:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
public function index(Request $request)
{
$search = trim($request->query('search', ''));
$course = trim($request->query('course', ''));
$students = DB::table('students')
->when($search !== '', function ($query) use ($search) {
$query->where('name', 'like', '%' . $search . '%');
})
->when($course !== '', function ($query) use ($course) {
$query->where('course', $course);
})
->orderBy('id', 'desc')
->paginate(10)
->withQueryString();
return view('students.index', compact(
'students',
'search',
'course'
));
}
Search form:
<form action="{{ route('students.index') }}" method="GET">
<input
type="text"
name="search"
placeholder="Student name"
value="{{ $search }}"
>
<select name="course">
<option value="">All Courses</option>
<option value="ADCA" {{ $course === 'ADCA' ? 'selected' : '' }}>
ADCA
</option>
<option value="Tally Prime" {{ $course === 'Tally Prime' ? 'selected' : '' }}>
Tally Prime
</option>
</select>
<button type="submit">Search</button>
<a href="{{ route('students.index') }}">Reset</a>
</form>
Both filters are combined using AND logic. A student must match the selected course and the name search when both filters have values.
Laravel provides different pagination methods depending on the needs of your application.
| Method | Purpose |
|---|---|
| paginate(10) | Includes total record count and page numbers. |
| simplePaginate(10) | Provides Previous and Next links without counting all matching records. |
| cursorPaginate(10) | Uses cursor-based navigation, useful for large ordered datasets. |
Simple pagination:
$students = DB::table('students')
->orderBy('id')
->simplePaginate(10);
Cursor pagination:
$students = DB::table('students')
->orderBy('id')
->cursorPaginate(10);
Cursor pagination requires a suitable ordering, typically using a unique column such as ID. Choose the pagination method based on whether you need page numbers, total counts, or efficient sequential navigation.
Build a student listing page with searching and pagination.
Challenge:
Add a total student count and allow the user to select how many records to display per page. Validate the requested page size against a small allowed list such as 10, 25, and 50 before passing it to paginate().
Use GET parameters for search and filters, and ensure the database table and column names match your project.
1. Which method displays records page by page with total record counts?
2. Which HTTP method is commonly used for search forms?
3. Which method preserves query-string parameters across pagination links?