LoginSignup
0
0

More than 3 years have passed since last update.

laravel 前ページ、次ページ

Posted at

laravel で 前後のページ、及びレコードを取得したい場合がある。
その時の書き方。



$next = Article::query()//単数形 User
    ->where('id','>',$request->id)
    ->orderBy('id','ASC')
    //NGユーザーは読み出さない
    ->whereDoesntHave('social', function ($q){
        $q->where('ng',1);
    })
    ->select('id','name')
    ->first();//最初の

//次ページある
if (!empty($next->id)) {
    pd($next->id);
    pd($next->name);
} else {

    pd("次ページはありません");

}



$prev = Article::query()//単数形 User
    ->where('id','<',$request->id)
    ->orderBy('id','DESC')
    //NGユーザーは読み出さない
    ->whereDoesntHave('social', function ($q){
        $q->where('ng',1);
    })
    ->select('id','name')
    ->first();//最初の

//次ページある
if (!empty($prev->id)) {
    pd($prev->id);
    pd($prev->name);
} else {

    pd("前ページはありません");

}



こんな感じ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0