Get Next And Previous Post Url In Laravel
Hi buddies,
I will show you an example of laravel getting the next and previous record. I’m going to show you how to get the next and previous record in laravel. you will learn laravel next and the previous record. This post will give you a simple example of laravel next previous record URL and link. Let's see below how to get previous and next records in the laravel query.
In this example, we will create a posts table and create a view route to display post details. On the details page, we will add the previous and next buttons to show the next post and the previous post. you can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.
You can see simply preview below:
Lets's Start
Step1 : Create Model
In the first step, We need to create attributes for the next and previous post URLs. So, we can get the post object for the next and previous posts. so let's add code as below and add some dummy records to your posts table:
app/Models/Post.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
/**
* Write code on Method
*
* @return response()
*/
public function getNextAttribute(){
return static::where('id', '>', $this->id)->orderBy('id','asc')->first();
}
/**
* Write code on Method
*
* @return response()
*/
public function getPreviousAttribute(){
return static::where('id', '<', $this->id)->orderBy('id','desc')->first();
}
}
Step 2: Create Route
In this step, We will create a route to show posts.
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('post/{post}',[PostController::class,'show'])->name('post.show');
Step 3: Create Controller
In this step, We will fetch the post to show.
app/Http/Controllers/PostController.php
namespace App\Http\Controllers;
use App\Http\Requests\StorePostRequest;
use App\Http\Requests\UpdatePostRequest;
use App\Models\Post;
class PostController extends Controller
{
/**
* Display the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function show(Post $post) {
return view('posts.show', compact('post'));
}
}
Step 4: Create view Files
In this step, we will create a blade file to display the post to the user with the next and previous post-URL.
resources/views/posts/show.blade.php
{{ $post->title }}
{{ $post->title }}
{!! $post->content !!}
@if (isset($post->previous))
Previous
{{ $post->previous->title }}
@endif
@if (isset($post->next))
Next
{{ $post->next->title }}
@endif
Now, We are ready to test our code. just run the artisan serve command.
php artisan serve
Now just open the given URL to view your post.
localhost:8000/post/{slug}
With this, All of our steps are completed.
Hope this post is helpful for you.