Laravel Get Next and Previous Record with URL Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi Dev,

I am going to show you example of laravel get next and previous record. I’m going to show you about how to get next and previous record in laravel. you will learn laravel next previous record. This post will give you simple example of laravel next previous record url and link. Let's see bellow example how to get previous and next record in laravel query.

In this example, we will create posts table and create view route to display post details. in details page we will add previous and next button to show next post and previous post. you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

You can see simply preview as bellow:

Preview:

Step 1: Create Post Model

In first step, we need to create posts route with next and previous attribute. so we can get next and previous record from object. so let's add code as bellow and add some dummy records to your posts table:

app/Models/Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Post extends Model

{

use HasFactory;

protected $fillable = [

'title',

'body',

'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 is step we need to create some routes for view posts.

routes/web.php

<?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('posts/{slug}',[PostController::class,'show'])->name('posts.show');

Step 3: Create Controller

in this step, you can put bellow code for post controller:

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function show($slug)

{

$post = Post::whereSlug($slug)->first();

return view('posts.show', compact('post'));

}

}

Step 4: Create Blade Files

here, we need to create blade files for show. so let's create one by one files:

resources/views/posts/show.blade.php

<!DOCTYPE html>

<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Get Previous and Next Record in Laravel - ItSolutionstuff.com</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container mt-5">

<h1>How to Get Previous and Next Record in Laravel - ItSolutionstuff.com</h1>

<h2>{{ $post->title }}</h2>

<p>{{ $post->body }}</p>

<div class="row">

<div class="col-6">

@if (isset($post->previous))

<a href="{{ route('posts.show', $post->previous->slug) }}">

<div> Previous</div>

<p>{{ $post->previous->title }}</p>

</a>

@endif

</div>

<div class="col-6">

@if (isset($post->next))

<a href="{{ route('posts.show', $post->next->slug) }}">

<div>Next</div>

<p>{{ $post->next->title }}</p>

</a>

@endif

</div>

</div>

</div>

</body>

</html>

Now we are ready to run our example. so run bellow command so quick run:

php artisan serve

Now you can open bellow URL on your browser:

localhost:8000/posts/{your_slug}

i hope it can help you...

Tags :
Shares