Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the data of the previous and next articles in Laravel

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to get the data of the previous article and the next article in Laravel". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope that this article "how to get the data of the previous article and the next article in Laravel" can help you solve the problem.

Question: how does the Eloquent ORM of Laravel get the next entry of the current record

Then, at that time, a brief solution was written in the answer. However, since this record of getting the next article and the previous one is often encountered in daily development, the most common scenario may be to get the previous article and the next article of an article. In fact, this is quite easy to implement in Laravel's Eloquent, but since Laravel does not provide us with the corresponding method directly, we have to use a small technique:

Get the previous article id

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

Basically, it can be said that it can be obtained in the same way. This id to get the next article is actually the opposite process, long live understanding.

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

The copy code is as follows:

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

Say a few more words

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

Add a published_at field to the articles table, where you can set the published_at field to a Carbon object, and then we can read whether or not to display the article according to published_at when we display it at the front end.

For example, a query statement:

Public function scopePublished ($query) {$query- > where ('published_at','

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report