SoFunction
Updated on 2025-04-07

Laravel Eloquent's id implementation explanation of the previous and next data

origin

First of all, the origin of the article comes from a question:

How to get the next item of the current record in Laravel's Eloquent ORM

I want to get the next item and change its active field to yes, but it seems that it cannot be obtained

$next_active = $ips->where("id", ">", $ips_get->id)->first();
$next_active->update(["active" => "yes"]);

Then, I briefly wrote a solution in the answer. However, since the record of obtaining the next and obtaining the previous one is still often encountered in daily development, the most common scenario may be to obtain the previous and next articles of an article. Actually, this isLaravelofEloquentIt's quite easy to implement, but becauseLaravelThere is no corresponding method provided directly to us, we have to use a small trick:

Get the previous article id

protected function getPrevArticleId($id)
    {
        return Article::where('id', '<', $id)->max('id');
    }

$id is the current articleid, we passmax()To get the current oneidThe small maximum value is the current valueidThe previous articleid

Get the next article id

protected function getNextArticleId($id)
    {
        return Article::where('id', '>', $id)->min('id');
    }

Basically, it can be said:The same is true. This gets the next articleidIn fact, it is a reverse process, long live understanding.

Once we get the ids of the previous and next articles, we can do whatever we want, for example:

$next_article = Article::find($this->getNextArticleId($article->id));

Say a few more words

If it is for the management of an article, we can actually do this:

GivearticlesAdd one to the tablepublished_atThe fields ofpublished_atSet the field to aCarbonWe can then display the object in the front-end according topublished_atLet's judge whether to display the article.

For example, query statement:

public function scopePublished($query)
    {
        $query-&gt;where('published_at','&lt;=',Carbon::now());
    }
//The above method is located in Article, I put the following query in ArticleController$articles = Article::latest('published_at')-&gt;published()...

View display

&lt;li class="previous"&gt;
@if($prev_article)
&lt;a href="/post/{{ $prev_article-&gt;slug }}" rel="external nofollow"  rel="prev"&gt;&lt;i class="fa fa-chevron-left"&gt;&lt;/i&gt;&lt;strong&gt;Previous article&lt;/strong&gt;&lt;span&gt;  {{ $prev_article-&gt;title }}&lt;/span&gt; &lt;/a&gt;
@endif
&lt;/li&gt;
&lt;li class="next"&gt;
@if($next_article &amp;&amp; $next_article-&gt;published_at &lt; Carbon\Carbon::now())
&lt;a href="/post/{{ $next_article-&gt;slug }}" rel="external nofollow"  rel="next"&gt;&lt;i class="fa fa-chevron-right"&gt;&lt;/i&gt;&lt;strong&gt;Next article&lt;/strong&gt; &lt;span&gt;  {{ $next_article-&gt;title }}&lt;/span&gt;&lt;/a&gt;
@endif
&lt;/li&gt;

The solution to deal with the previous and next articles of the article has been completed.

The above is the detailed explanation of the implementation of the id of Laravel Eloquent to retrieve the previous and next data. For more information about the id of Laravel Eloquent to retrieve the data, please pay attention to my other related articles!