How to get the next and previous records with URL using Laravel
In this tutorial, we will learn How to get the next and previous records with URLs using Laravel. In this tutorial, I’m showing you how to get the next and previous records with the URL and link in Laravel. We required a posts table and create a view route to display post details. post details page we will add the next and previous buttons to show the next post and the previous post.
You can watch the preview below:
Create Post-migration
First, we create post-migration. using the below command:
php artisan make:migration create_posts_table
<?php #database\migrations\2021_11_11_154211_create_posts_table.php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title')->nullable(); $table->text('body')->nullable(); $table->string('slug')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
Create Post Model
We create a post model by creating a post route with the next and previous attributes. so we can get the next and previous records from the object. and we add code as bellow and add dummy records to the posts table:
<?php #app/Models/Post.php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Posts extends Model { use HasFactory; protected $fillable = [ 'title', 'body', 'slug' ]; public function getNextAttribute(){ return static::where('id', '>', $this->id)->orderBy('id','asc')->first(); } public function getPreviousAttribute(){ return static::where('id', '<', $this->id)->orderBy('id','desc')->first(); } }
Create Controller
Simple you can put bellow code for post controller:
<?php #app/Http/Controllers/PostController.php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Posts; class PostController extends Controller { public function show($slug) { $post = Posts::whereSlug($slug)->first(); return view('posts.show', compact('post')); } }
Define Route
Now, create some routes for viewing posts in routes/web.php
.
<?php #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('posts/{slug}',[PostController::class,'show'])->name('posts.show');
Create Blade Files
we can create blade files for the show inside the view folder.
resources/views/posts/show.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to get next and previous record with url using Laravel - devnote.in</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>How to get next and previous record with url using Laravel - devnote.in</h1>
<h3>{{ $post->title }}</h3>
<p>{{ $post->body }}</p>
<div class="row">
<div class="col-md-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-md-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.
http://127.0.0.1:8000/posts/{your_slug}
example: http://127.0.0.1:8000/posts/export-mysql-database-to-sql-file-using-php