Laravel的Eloquent ORM 怎么获取当前记录的下一条
1.取得上一篇的文章id
protected function getPrevChmsId($id)
{
//$id就是当前文章的id
//我们通过max()来取得比当前id小的最大值,也就是当前id的前一篇文章的id。
return Chm::where('id','<',$id)->max('id');
}
$id就是当前文章的id,我们通过max()来取得比当前id小的最大值,也就是当前id的前一篇文章的id。
2.取得上一篇的文章id
protected function getNextChmId($id)
{
//这个取得下一篇文章的id
return Chm::where('id','>',$id)->min('id');
}
我们取得上一篇和下一篇的文章id之后,就能获取上一篇与下一篇文章数据
3.获取展示数据
$prev_chm = Chm::find($this->getPrevChmsId($id));
$next_chm = Chm::find($this->getNextChmId($id));
4.View展示:
<div class="navigation">
@if($prev_chm)
<div class="alignleft">上一篇:<a href="{{ route('jqueryshow',['show'=>$prev_chm->id])}}">{{ $prev_chm->title }}</a></div>
@endif
@if($next_chm)
<div class="alignright">下一篇:<a href="{{ route('jqueryshow',['show'=>$next_chm->id])}}">{{ $next_chm->title }}</a></div>
@endif
</div>