Home Backend Development PHP Tutorial Laravel 中获取上一篇和下一篇数据_php实例

Laravel 中获取上一篇和下一篇数据_php实例

Jun 07, 2016 pm 05:12 PM
laravel

首先文章的起源来与SF上面的一个问题:

Laravel的Eloquent ORM 怎么获取当前记录的下一条

然后,当时在答案里面简单写了一下解决方案。不过由于这个取得下一条和取得上一条的记录其实在日常的开发当中还是会经常遇到,最常见的场景可能就是取得一篇文章的上一篇文章和下一篇文章了。其实这个在Laravel的Eloquent中实现还是挺容易的,不过由于Laravel并没有直接提供给我们相应的方法,我们得使用一个小小的技巧:

取得上一篇的文章id

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

Copy after login

$id就是当前文章的id,我们通过max()来取得比当前id小的最大值,也就是当前id的前一篇文章的id。

取得上一篇的文章id

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

Copy after login

基本上可以说是:同理可得。这个取得下一篇文章的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','<=',Carbon::now());
  }
Copy after login

//以上方法位于Article中,下面的查询我放在了ArticleController中

$articles = Article::latest('published_at')->published()...
Copy after login

View展示:

<li class="previous">
@if($prev_article)
<a href="/post/{{ $prev_article->slug }}" rel="prev"><i class="fa fa-chevron-left"></i><strong>上一篇</strong><span> {{ $prev_article->title }}</span> </a>
@endif
</li>
<li class="next">
@if($next_article && $next_article->published_at < Carbon\Carbon::now())

<a href="/post/{{ $next_article->slug }}" rel="next"><i class="fa fa-chevron-right"></i><strong>下一篇</strong> <span> {{ $next_article->title }}</span></a>
@endif
</li>

Copy after login

处理文章的前一篇和后一篇的解决方案已完成。

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

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)

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Laravel - Pagination Customizations Laravel - Pagination Customizations Aug 27, 2024 am 10:51 AM

Laravel - Pagination Customizations - Laravel includes a feature of pagination which helps a user or a developer to include a pagination feature. Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatical

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

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

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 - Dump Server Laravel - Dump Server Aug 27, 2024 am 10:51 AM

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

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

See all articles