Obtain the data of the previous and next articles in Laravel.

Source: Internet
Author: User
This article describes how to obtain data from the previous and next articles in Laravel. For more information, see

This article describes how to obtain data from the previous and next articles in Laravel. For more information, see

First of all, the origin of this article is related to the above question of SF:

How can Laravel's Eloquent ORM obtain the next record?

Then, I wrote a simple solution in the answer. However, this acquisition of the next article and the previous record are often encountered in daily development. The most common scenario may be the acquisition of the previous article and the next article. In fact, this implementation in Laravel's Eloquent is quite easy, but since Laravel does not directly provide us with the corresponding method, we have to use a small trick:

Obtain the id of the previous Article

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

$ Id is the id of the current article. We use max () to obtain the maximum value smaller than the current id, that is, the id of the previous Article of the current id.

Obtain the id of the previous Article

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

It can basically be said that the same can be achieved. This acquisition of the id of the next article is actually an opposite process.

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

The Code is as follows:

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

Say a few more

For the management of an article, we can actually do this:

Add a published_at field to the articles table. Here, you can set the published_at field to a Carbon object. Then, when presenting the article on the front end, you can determine whether to display the article based on published_at.

For example, the query statement:

Public function scopePublished ($ query) {$ query-> where ('ed hed _ at', '<=', Carbon: now ());}

// The above method is located in Article, and the following query is stored in ArticleController.

$ Articles = Article: latest ('hhed _ at')-> published ()...

View display:

  • @ If ($ prev_article) slug} ">Previous{{$ Prev_article-> title }}@ endif
  • @ If ($ next_article & $ next_article-> published_at <Carbon \ Carbon: now () slug} ">Next article{{$ Next_article-> title }}@ endif
  • The solution for processing the previous and later articles has been completed.

    The above is all the content of this article. I hope you will like it.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.