Home Laravel How to Create Pretty URL Pagination In Laravel ?

How to Create Pretty URL Pagination In Laravel ?

by Shahriar Sagor

Today now in this article, i will talk about the laravel pagination pretty url. So in this tutorial i will give you a simple example of how we can make pagination pretty url in laravel. Here we know about Pretty URL Pagination In Laravel. From here you’ll learn about laravel paginator pretty url. So here you will learn laravel pagination with pretty url full example step by step.

We can also use this example with laravel 6, laravel 7, laravel 8 and laravel 9 any version.

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

So let’s start and See the bellow example

Step 1: Install Laravel

Now in this step is not needed; however, if you don’t have then need to created the laravel application, for create new application then need to execute the below command:

composer create-project laravel/laravel example-app
Step 2: Generate Dummy Users

So in this step, i will generate some dummy users then we can make it pagination. Let’s need to run tinker command and add the new users:

php artisan tinker
User::factory()->count(40)->create()
Step 3: Create Routes

Now in this step, i will create two routes for the users and another one for the pretty URL. So let’s add it.

routes/web.php

<?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']);
Route::get('users/page/{page}', [UserController::class, 'index'])->name('users.index');
Step 4: Create Controller

Now in this step, we need to create a new controller as UserController with index(). So here i will write logic of pagination on it. So let’s update the follow code:

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, $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

This is the last step, so let’s create the users.blade.php for create form with the display validation message and put the following code:

resources/views/users.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Pagination Pretty URL Example - CodingsPoint.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 - CodingsPoint.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:

That’s good all the needed steps have been done, now we have to type the given below command and then hit enter to run the Laravel application:

php artisan serve

Read Also: How to Use Google Recaptcha V2 In Php ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like