Laravel Pagination Get First Page Tutorial

Laravel 8 , Laravel 7

Nicesnippets

120

04-05-2021


Laravel Pagination Get First Page Tutorial

Hi guys, Today i will explained to the Laravel Pagination Get First Page Tutorial in your laravel project.you can easy and simply Pagination first Button in laravel.we can add first link on pagination using simplePaginate() in laravel 6, laravel 7 and laravel 8 application. laravel provide new eloquent method simplePaginate() for adding simple pagination with only first button link.

The Laravel Pagination Get First Page is so easy to use.so you can just follow my step by step and learn Laravel Pagination Get First Page Tutorial.

So let's start to the example and follow to the my all step.

Solution


User::paginate(10)->onFirstPage()

Step 1: Create a User Controller

Next you can require to the User Controller so create a User Controller in just following command through.

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index(Request $request)

{

$users = User::paginate(10)->onFirstPage();

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

}

}

Step 2: Add Blade File

Last step to create a users.blade.php file and use this code.

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Pagination Get First Page Tutorial</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<body>

<div class="container">

<h1>Laravel Pagination Get First Page Tutorial</h1>

<table class="table table-bordered">

<tr>

<th>ID</th>

<th>Name</th>

</tr>

@foreach($users as $user)

<tr>

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

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

</tr>

@endforeach

</table>

{{ $users->links() }}

</div>

</body>

</html>

Step 3: Create Route

Last step to create a route in web.php file and use this code.

routes/web.php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

Ok, now you have to create some dummy records on users table.

So, finally we are done with our code we can get below output.

php artisan serve

Browser url run : http://localhost:8000/users

output: