Laravel 中获取上一篇和下一篇数据
这篇文章主要介绍了Laravel 中获取上一篇和下一篇数据的相关资料,需要的朋友可以参考下
首先文章的起源来与SF上面的一个问题:
Laravel的Eloquent ORM 怎么获取当前记录的下一条
然后,当时在答案里面简单写了一下解决方案。不过由于这个取得下一条和取得上一条的记录其实在日常的开发当中还是会经常遇到,最常见的场景可能就是取得一篇文章的上一篇文章和下一篇文章了。其实这个在Laravel的Eloquent中实现还是挺容易的,不过由于Laravel并没有直接提供给我们相应的方法,我们得使用一个小小的技巧:
取得上一篇的文章id
protected function getPrevArticleId($id) { return Article::where('id', 'max('id'); }
$id就是当前文章的id,我们通过max()来取得比当前id小的最大值,也就是当前id的前一篇文章的id。
取得上一篇的文章id
protected function getNextArticleId($id) { return Article::where('id', '>', $id)->min('id'); }
基本上可以说是:同理可得。这个取得下一篇文章的id其实就是一个相反的过程,理解万岁。
一旦我们取得上一篇和下一篇的文章id之后,我们就可以随心所欲了,比如:
复制代码 代码如下:
$next_article = Article::find($this->getNextArticleId($article->id));
多说两句
那如果是对于一个文章的管理来说,我们其实可以这么做:
给articles表中增加一个published_at的字段,,这里可以将published_at字段设置为一个Carbon对象,然后我们在前端展示的时候就可以根据published_at来判读是否将文章展示出来。
比如说查询语句:
public function scopePublished($query) { $query->where('published_at','
//以上方法位于Article中,下面的查询我放在了ArticleController中
$articles = Article::latest('published_at')->published()...
View展示:
处理文章的前一篇和后一篇的解决方案已完成。
以上所述就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...
