dongqian3750 2017-07-15 19:07
浏览 49

Laravel 5.3上一页和下一页记录链接

I have found a solution for next and previous records links in database.But the solution is only working good for next record.When i press back link , it shows first record.Can you help me to find a solution for showing biggest number of previous records?

//Model file

public static function findNext($id)
{
    return static::where('id','>',$id)->first();
}
public static function findPrevious($id)
{
    return static::where('id','<',$id)->first();
}

//Controller file

public function show($id)
{
$c = C::findOrFail($id);
$nextUser = C::findNext($id);
$previousUser = C::findPrevious($id);
    return view('c.show', compact('c',$c))->with('nextUser',$nextUser)-
>with('previousUser',$previousUser);
    }

//View file

@if($previousUser)
<a href="{{ URL::to( 'c/show/' . $previousUser->id . '/previous') 
}}">Previous</a> | 
@endif
@if($nextUser)
<a href="{{ URL::to( 'c/show/' . $nextUser->id . '/next') }}">Next</a>
@endif

//Route file

Route::get('c/show/{id}', 'CController@show');
Route::get('c/show/{user}/next',function($id){
$nextUser=C::findNext($id);
return Redirect::to('c/show/'.$id);
});
Route::get('c/show/{user}/previous',function($id){
$nextUser=C::findPrevious($id);
return Redirect::to('c/show/'.$id);
});
  • 写回答

1条回答 默认 最新

  • dousi2251 2017-07-15 20:02
    关注

    Try with the orderBy() method and a descending direction:

    public static function findPrevious($id)
    {
        return static::where('id', '<', $id)
            ->orderBy('id', 'desc')
            ->first();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大