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
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
Recommanded Articles
- How to create a multilevel category and subcategory in Laravel
- How to check YouTube video exist Laravel validation
- Multiple user roles authentication Laravel 8
- Deploy Laravel project from local to production server
- Make custom pagination URL in Laravel without query strings
- Web Scraping in Laravel using Goutte
- Insert values during migration run laravel
- Validation for string characters only with custom message in Laravel
- Add new columns in a table Laravel
- How to create foreign key constraints in Laravel
Latest Comments
Roi Hirshberg
16 Dec 2020Rohit sekhri
17 Dec 2020Shashank mehta
21 Dec 2020Ricky Dev
22 Dec 2020Josaf Nayar
27 Dec 2020Shamy
28 Dec 2020Shank Rocks
01 Feb 2021Rohan
15 Jan 2023