Laravel Pagination Pretty URL Example

Hello all! In this article, we will talk about laravel pagination pretty url. This tutorial will give you simple example of how to make pagination pretty url in laravel. you’ll learn laravel paginator pretty url. Here you will learn laravel pagination with pretty url example.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.

Laravel provides pagination default and it’s working fine. But they generate URL with query strings and if you need to generate pretty url(seo friendly URL) for your pagination, Then I will help you with how you can do it.

Laravel Default Pagination URL:

http://localhost:8000/users?page=1

http://localhost:8000/users?page=2

http://localhost:8000/users?page=3

Laravel Pagination with Pretty URL:

http://localhost:8000/users/page/1

http://localhost:8000/users/page/2

http://localhost:8000/users/page/3

If you want to generate pretty url then just follow below step to done this example:

Preview:

Laravel Pagination Pretty URL Example

Let’s follow below steps:

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Generate Dummy Users

In this step, we will generate some dummy users so we can make it pagination, so let’s run tinker command and add new users:

php artisan tinker

User::factory()->count(40)->create()

Step 3: Create Routes

In this step, we will create two routes for users and another for pretty URL. so let’s add it.

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersUserController;

/*

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

| 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']);

Route::get('users/page/{page}', [UserController::class, 'index'])->name('users.index');

Step 4: Create Controller

In this step, we have to create new controller as UserController with index(). we will write logic of pagination on it. so let’s update follow code:

app/Http/Controllers/UserController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsUser;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index(Request $request, $page = 1)

{

$paginate = 4;

$skip = ($page * $paginate) - $paginate;

$prevURL = $nextURL = '';

if ($skip > 0){

$prevURL = $page - 1;

}

$users = User::latest()

->skip($skip)

->take($paginate)

->get();

if($users->count() > 0){

if($users->count() >= $paginate){

$nextURL = $page + 1;

}

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

}

return redirect('/');

}

}

Step 5: Create View File

In Last step, let’s create users.blade.php for create form with display validation message and put following code:

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Pagination Pretty URL Example - ItSolutionStuff.com</title>

<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 Pagination Pretty URL Example - ItSolutionStuff.com</h1>

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

<thead>

<tr>

<th>ID</th>

<th>Name</th>

<th>Email</th>

<th>Action</th>

</tr>

</thead>

<tbody>

@foreach($users as $user)

<tr>

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

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

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

<td>

<a

href="javascript:void(0)"

id="show-user"

class="btn btn-info"

>Show</a>

</td>

</tr>

@endforeach

</tbody>

</table>

<div class="text-center">

@if($prevURL)

<a class="btn btn-primary m-10 leftbtn" href="{{ route('users.index', $prevURL) }}"><i class="fa fa-angle-left" aria-hidden="true"></i> Previous</a>

@endif

@if($nextURL)

<a class="btn btn-primary m-10 rightbtn" href="{{ route('users.index', $nextURL) }}">Next <i class="fa fa-angle-right" aria-hidden="true"></i> </a>

@endif

</div>

</div>

</body>

</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/users

I hope it can help you…

Leave a Comment