LoginSignup
0
0

More than 3 years have passed since last update.

【超自分用】Laravelのページネーションのまとめ【随時更新予定】

Posted at

導入

以下のコマンド導入可能

php artisan vendor:publish --tag=laravel-pagination

resources/views/vendor/pagination/の中にいろいろテンプレファイルができればおっけー

実装例

Contoroller側

xxxContoroller.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Book;

class BookListController extends Controller
{
    public function index()
    {
        $books = Book::join('users', 'books.create_user', '=', 'users.id')
            ->select('books.*', 'users.id', 'users.name')
            ->orderByRaw('books.created_at DESC')
            ->paginate(10);

        return view('bookList', ['books' => $books]);
    }
}

いろいろjoinとかしてますが、一旦無視してください。
>paginate(10);で1ページに表示する数を設定

view側

                @if (count($books) > 0)
                <table class="table table-striped task-table">
                    <thead>
                        <th>書籍タイトル</th>
                        <th>投稿者</th>
                        <th>投稿日</th>
                    </thead>
                    <tbody>
                        @foreach ($books as $book)
                        <tr>
                            <td class="table-text">
                                <div>{{ $book->title }}</div>
                            </td>
                            <td class="table-text">
                                <div>{{ $book->name }}</div>
                            </td>
                            <td class="table-text">
                                <div>{{ $book->created_at }}</div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
                {{ $books->links('vendor.pagination.bootstrap-4') }}
                @endif

いろいろごちゃごちゃ書いてますが無視してください
{{ $books->links('vendor.pagination.bootstrap-4') }}でブートストラップのデザインが適用可能
導入した際のファイルが実際のスタイルを記述している
image.png

他にもいろんなテンプレがあるので試してみてください
もちろん自分で書き換えることも可能

まとめ

めっちゃ便利。忘れたくない

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