Laravel Get Next and Previous Record with URL Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Summernote Image Upload Example
- Laravel Image Upload with Spatie's Media Library Example
- Laravel Google Autocomplete Address Example
- Laravel Eloquent Select Single Column to Array Example
- How to Add Social Media Share Buttons in Laravel?
- Laravel File Manager Tutorial Step by Step
- How to Create Dynamic Navigation Bar in Laravel?
- Laravel Migration Custom Foreign Key Name Example
- Laravel 8 Model Observers Tutorial Example
- Drag & Drop File Uploading using Laravel 8 Dropzone JS
- Laravel 8 Inertia JS CRUD with Jetstream & Tailwind CSS
- Laravel 8 Queue Step by Step Tutorial Example