LoginSignup
1
2

More than 1 year has passed since last update.

Laravelで配列をページネーションする

Posted at

laravelで配列をページネーションする方法の備忘録です。
環境は Laravel v8.83.6 (PHP v7.4.28) です。

Controller

app/Http/Controllers/HomeController.php
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $arr = [
            ['dep'=>'06:09', 'arr'=>'08:12', 'train'=>'とき300号', 'dest'='東京'],
            ['dep'=>'06:34', 'arr'=>'08:48', 'train'=>'とき302号', 'dest'='東京'],
            ['dep'=>'06:58', 'arr'=>'09:00', 'train'=>'とき304号', 'dest'='東京'],
            ['dep'=>'07:19', 'arr'=>'09:40', 'train'=>'とき306号', 'dest'='東京'],
            ['dep'=>'07:50', 'arr'=>'10:04', 'train'=>'とき308号', 'dest'='東京'],
            ['dep'=>'08:26', 'arr'=>'10:28', 'train'=>'とき310号', 'dest'='東京'],
            ['dep'=>'09:05', 'arr'=>'10:43', 'train'=>'とき312号', 'dest'='東京'],
        ];
  
        $coll = collect($arr);
  
        $data = $this->paginate($coll);
   
        return view('home', compact('data'));
    }


    private function paginate($items, $perPage = 5, $page = null, $options = [])
    {
        $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
        $items = $items instanceof Collection ? $items : Collection::make($items);
        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
    }

}

View

resources/views/home.blade.php
<div class="container">
    <table class="table table-bordered">
        <tr>
            <th>出発</th>
            <th>到着</th>
            <th>列車</th>
            <th>行先</th>
        </tr>
        @foreach($data as $elem)
        <tr>
            <td>{{ $elem['dep'] }}</td>
            <td>{{ $elem['arr'] }}</td>
            <td>{{ $elem['train'] }}</td>
            <td>{{ $elem['dest'] }}</td>
        </tr>
        @endforeach
    </table>
</div>
   
{{ $data->links() }}

ページネーションされた画面が表示されましたがこのままでは2ページ目が表示されません。

$this->paginate()の第4パラメータにPathを指定します。
2ページ目が正しく表示されます。

$data = $this->paginate($collect_list, 5, null, ['path'=>'/home']);

LengthAwarePaginatorへのパラメータ

[1ページ分のコレクション] = new LengthAwarePaginator(
            [全コレクション]->forPage([現在のページ番号],[1ページ当たりの表示数]),
            [コレクションの件数],
            [1ページ当たりの表示数],
            [現在のページ番号],
            [オプション(ページの遷移先パス)]
        );

ちなみに

Laravel8ではTailwindがデフォルトの設定になっているため次のようにBootstrapを使えるように設定しました。

app\Providers\AppServiceProvider.php

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Paginator::useBootstrap();  //←追加(Bootstrapのuse宣言)
    }

コントローラに次を追加する。

use Illuminate\Pagination\Paginator;

参考記事

この記事は以下の情報を参考にして記述しました。

1
2
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
1
2