Make custom pagination URL in laravel without query strings

Make custom pagination URL in laravel without query strings

There are multiple libraries in laravel which will used to create pretty pagination URL, but in this article I will show you how you can achieve this functionality without using any library. For that first we will create a model, controller and database migration. We can create these all by executing a single command. Run below command to create model, controller and a migration.

php artisan make:model Posts -mcr

After creating your model, controller and migration, open your migration file and add columns which you want. I'm going to add four columns in my post table. Go to your laravel project -> database -> migration folder and open recently create file. On my system, the name of the file name is 2020_03_07_100107_create_posts_table.php. Copy below code and paste in your file.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
          $table->bigIncrements('id');
$table->string('title')->unique();
$table->longText('content')->nullable();
$table->string('image')->nullable();
$table->text('category')->nullable();
$table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

After pasted the above code in your migration file, execute the command to create posts tables with above columns.

php artisan migrate

Table will created in your database, after that add some entries in your post table. Add more than 10 posts in your post tables. Not Go to laravel project folder -> app ->Http -> Controllers. Open file PostsController.php and pasted below function in it. In below function i will display 8 records on a pagination page, you can change it according to your requirement.

public function allPosts($id){
$paginate = 8; $skip = ($id*$paginate)-$paginate;
$prevUrl = $nextUrl = ''; if($skip>0){ $prevUrl = $id - 1; } $posts = Posts::orderBy('id', 'desc')->skip($skip)->take($paginate)->get();
if($posts->count()>0){ if($posts->count()>=$paginate){
$nextUrl = $id + 1; } return view('posts', compact('posts', 'prevUrl', 'nextUrl')); } return redirect('/blogs'); }

Add below route in web.php

Route::get('/posts/{id}', 'PagesController@allPosts')->name('allPosts');

After added route create a template blade file posts.blade.php in laravel project folder -> resources -> views and pasted below code in it.

<div class="container">
<div class="container">
@foreach($posts as $post)
<div class="col-md-6 col-sm-6">
<img src="{{$post->image}}">
<h3>{{$post->title}}</h3>
<p>{{$post->content}}</p>
<small>{{$post->category}}</small>
</div>
@endforeach
<div class="pagination text-center">
@if($prevUrl)<a class="btn btn-info leftbtn" href="/all/blogs/{{$prevUrl}}"><i class="fa fa-angle-left" aria-hidden="true"></i> Previous</a>@endif
@if($nextUrl)<a class="btn btn-info rightbtn" href="/all/blogs/{{$nextUrl}}">Next <i class="fa fa-angle-right" aria-hidden="true"></i> </a>@endif
</div>
</div>
</div>

 LIVE DEMO: DEMO LINK

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Roi Hirshberg
Roi Hirshberg
15 Dec 2020

Pagination URL SEO issue is fixed after using your code in my laravel store. Really superbbbb

Rohit sekhri
Rohit sekhri
16 Dec 2020

Thank You! working like a charm

Shashank mehta
Shashank mehta
20 Dec 2020

working for me too. Thanksss

Ricky Dev
Ricky Dev
21 Dec 2020

Really works. Thanks

Josaf Nayar
Josaf Nayar
27 Dec 2020

Perfect! Thank you

Shamy
Shamy
27 Dec 2020

Great

Shank Rocks
Shank Rocks
31 Jan 2021

Great post

Add your comment