search
Homephp教程php手册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
    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

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.