[Laravel] ページネーションの簡単なサンプル

スポンサーリンク

コントローラ

public function index(Request $request): View
{
  $items = Item::orderBy('id', 'desc')->paginate(50);
  return view('items.index', ['items' => $items]);
}

ビュー

@if ($items->hasPages())
  全{{ $items->total() }}件中{{ $items->firstItem() }}〜{{ $items->lastItem() }}件目を表示中
@endif
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>アイテム</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($items as $item)
            <tr>
                <td>{{ $item->id }}</td>
                <td>{{ $item->name }}</td>
            </tr>
        @endforeach
    </tbody>
</table>
@if ($items->hasPages())
  {{ $items->links() }}
@endif

おすすめ書籍

コメント