Laravel中实现上一篇下一篇实现
本帖最后由 匿名 于 2022-11-30 16:22 编辑比如我们有一个article的表,存着我们id title 还有我们content,
同时有一个对应的Article model 来操作我们的Article库。
首先获得当前的ID $id,上一篇的id可以直接用Eloquent Model来实现
$prev_article = Article::where('id','<',$id)->orderBy('id','desc')->fitst();
$next_article = Article::where('id','>',$id)->orderBy('id','asc')->first();如果只是想获取文章的id 直接通过Laravel中max方法来实现
$prev_id = Article::where('id','<',$id)->max('id');
$next_id = Article::where('id','>',$id)->min('id');public function al_xq(Request $request)
{
if($request->isMethod('GET'))
{
$fl= DB::table('cb_fwxmfl')->orderBy('id','desc')->get();
$res = Anli::where(['id'=>$request->id])->first();
$al= DB::table('cb_anli_fenlei')->orderBy('id','desc')->get();
$s = Anli::where('id','<',$request->id)->orderBy('id','desc')->first(); //上一篇
$x = Anli::where('id','>',$request->id)->orderBy('id','asc')->first(); //下一篇
return view('m/al_xq',['fl'=>$fl,'res'=>$res,'al'=>$al,'s'=>$s,'x'=>$x]);
}
}前端代码为:
<div class="main-desc">
<blockquote>{!! htmlspecialchars_decode($res->content) !!}</blockquote>
<div class="piece">
<a href=" {{ route('al_xq',['id'=>$s->id]) }} ">
<i class="fa fa-angle-double-left fa-fw" aria-hidden="true"></i>
<span>上一篇:<span>{{$s->name}}</span></span>
</a>
<a href="{{ route('al_xq',['id'=>$s->id]) }}">
<span>下一篇:<span></span>{{$x->name}}</span>
<i class="fa fa-angle-double-right fa-fw" aria-hidden="true"></i>
</a>
</div>
</div>
页:
[1]