In Laravel, you often need to navigate through records in a database, particularly when creating paginated views or detail pages for items such as blog posts, products, or user profiles. One common requirement is to provide links to the next and previous records. This article will show you how to achieve this in a simple and straightforward manner.
Prerequisites
To follow along with this tutorial, you should have:
- A basic understanding of Laravel.
- A Laravel project set up.
- A database table with some records. We’ll use a
posts
table for demonstration.
Setting Up the Posts Model and Migration
First, let’s set up the Post
model and its migration. Run the following Artisan commands:
php artisan make:model Post -m
This command creates a Post
model and a migration file for the posts
table. Open the migration file located in database/migrations/
and modify it to include the necessary columns:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
Run the migration to create the table:
php artisan migrate
Creating Some Sample Data
To have some data to work with, let’s create a seeder. Run the following command:
php artisan make:seeder PostSeeder
In the PostSeeder
file located in database/seeders/
, add some sample data:
use Illuminate\Database\Seeder;
use App\Models\Post;
class PostSeeder extends Seeder
{
public function run()
{
Post::truncate();
Post::create(['title' => 'First Post', 'content' => 'Content for the first post.']);
Post::create(['title' => 'Second Post', 'content' => 'Content for the second post.']);
Post::create(['title' => 'Third Post', 'content' => 'Content for the third post.']);
}
}
Run the seeder to populate the database:
php artisan db:seed --class=PostSeeder
Getting the Next and Previous Records
Let’s move on to the main part of this tutorial: fetching the next and previous records. We’ll do this in a controller.
Create a controller using the following command:
php artisan make:controller PostController
In the PostController
, add a method to show a single post along with links to the next and previous posts:
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function show($id)
{
$post = Post::findOrFail($id);
$nextPost = Post::where('id', '>', $post->id)->orderBy('id')->first();
$previousPost = Post::where('id', '<', $post->id)->orderBy('id', 'desc')->first();
return view('posts.show', compact('post', 'nextPost', 'previousPost'));
}
}
In this method, we fetch the current post by its ID. Then, we use the where
method to find the next and previous posts based on the current post’s ID.
Creating the View
Create a view file named show.blade.php
in the resources/views/posts/
directory:
<!DOCTYPE html>
<html>
<head>
<title>{{ $post->title }}</title>
</head>
<body>
<h1>{{ $post->title }}</h1>
<p>{{ $post->content }}</p>
@if($previousPost)
<a href="{{ url('posts/' . $previousPost->id) }}">Previous Post: {{ $previousPost->title }}</a>
@endif
@if($nextPost)
<a href="{{ url('posts/' . $nextPost->id) }}">Next Post: {{ $nextPost->title }}</a>
@endif
</body>
</html>
This view displays the current post’s title and content, along with links to the next and previous posts if they exist.
Setting Up Routes
Finally, let’s define the route for showing a post. Add the following route to your web.php
file:
use App\Http\Controllers\PostController;
Route::get('posts/{id}', [PostController::class, 'show']);
Testing the Functionality
Start the Laravel development server:
php artisan serve
Visit http://localhost:8000/posts/1
in your browser. You should see the first post with links to the next and previous posts as applicable.
Conclusion
In this tutorial, we covered how to navigate through records in a Laravel application by creating links to the next and previous records. This approach can be adapted to various models and use cases in your application. Happy coding!
- Check our tools small Tools
- Check our tools website Word count