Home php教程 php手册 Laravel 中获取上一篇和下一篇数据

Laravel 中获取上一篇和下一篇数据

Jun 06, 2016 pm 07:51 PM
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展示:

  • @if($prev_article) 上一篇 {{ $prev_article->title }} @endif
  • @if($next_article && $next_article->published_at slug }}">下一篇 {{ $next_article->title }} @endif
  • 处理文章的前一篇和后一篇的解决方案已完成。

    以上所述就是本文的全部内容了,希望大家能够喜欢。

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

    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...

    In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

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

    How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

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

    Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

    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...

    Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

    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->

    Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

    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 effectively check the validity of Redis connections in Laravel6 project? How to effectively check the validity of Redis connections in Laravel6 project? Apr 01, 2025 pm 02:00 PM

    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...

    Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Apr 01, 2025 pm 12:21 PM

    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...

    See all articles