How to Get Previous and Next Record in Laravel
In this post we will give you information about How to Get Previous and Next Record in Laravel. Hear we will give you detail about How to Get Previous and Next Record in Laravel And how to use it also give you demo for it if it is necessary.
A really common task is to display links for the previous and next record typically you find these on blogs, when viewing the post a previous title and link are displayed and the same for the next post, as long as they are previous and next posts. In this post, I’ll explain how to create these.
There are several ways to implement this functionality and the simplest way is to use the Model Id. Since by right an Id auto increment, we can assume that the newest post will always have a higher number than the oldest post.
Laravel gets previous & next record, data, or URL examples. This tutorial will help us learn how to get the next/previous data or record in laravel application throughout this comprehensive tutorial.
How to Get Previous and Next Record in Laravel
Here are the quintessential steps you needed to follow to get the next or previous data and records from the database table in the laravel.
Get Next Record in Laravel
Get the next record or data from the database in the laravel app. We have one table name posts, we want to fetch the next record or data from the database table in laravel. So you can use the below eloquent query for that:
$next_record = Post::where('id', '>', $post->id)->orderBy('id')->first();
As you can see, we have defined the where()
, first()
, orderBy()
likewise first()
eloquent queries to get the next posts or records from the database.
Checkout Tool: Campaign URL Builder
Get Previous Record in Laravel
Define the $previous
variable, along with the Blog model. Set the where()
clause, pass the id and blog id properties and define the orderBy and first queries to fetch the previous records and post URL or slug from the database.
$previous_record = Blog::where('id', '<', $blog->id)->orderBy('id','desc')->first();
Access Prev / Next Records from Laravel Blade View
Open the blade view template; you can get the idea from the below code example to show the previous and next blog posts with their respective slug and URLs.
<!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 Previous and Next Record in Laravel</title>
</head>
<body>
<div >
<div >
<div >
@if (isset($previous_record))
<a href="{{ url($previous_record->url) }}">
<div> Previous</div>
<p>{{ $previous_record->title }}</p>
</a>
@endif
</div>
<div >
@if (isset($next_record))
<a href="{{ url($next_record->url) }}">
<div>Next</div>
<p>{{ $next_record->title }}</p>
</a>
@endif
</div>
</div>
</div>
</body>
</html>
Thank you for reading this blog.
Also see: How to change the URI (URL) for a remote Git repository?
. .
Hope this code and post will helped you for implement How to Get Previous and Next Record in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs