3
5

More than 5 years have passed since last update.

Laravel:ページネーション機能を追加

Last updated at Posted at 2018-10-04

概要

ページの下の方にある
<< 1 2 3 >>
みたいなアレ

- 動作確認環境

  • PHP 7.1
  • Laravel 5.7

使い方

▼ View に埋め込む

- 書き方

こんな感じ
{{ モデル名->links() }}

Viewの表示したい場所に埋め込む。

▼ Controllerを直す

- 基本の書き方

括弧内は1ページ中に表示する件数。
pageじゃなくてpagiなのが注意点。

基本の書き方
- $posts = Post::all();
+ $posts = Post::paginate(5);

例)

app/Http/Controllers/UserController.php
    public function index()
    {
        //ユーザーを一覧表示
-       $users = User::all();
+       $users = User::paginate(5);
        return view('users.index')->with('users', $users);
    }

- 両サイドの件数を指定する書き方

Laravel 5.7 でonEachSide() というメソッドが追加された。
デフォルトで3のところ、自由に設定できるように。

$posts = Post::paginate(10)->onEachSide(5);

// output
// .. 3 4 5 6 7 [8] 9 10 11 12 13 ...

参考: :link: readouble.comペジネーションリンク

3
5
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
3
5