LoginSignup
42
52

More than 5 years have passed since last update.

laravelのページネーションでpage以外のGetパラメータを引き継がせる方法

Last updated at Posted at 2015-03-26

laravelのページネーション実装方法メモ

HogeController.php
<?php
...
$from_date = Input::get('from_date');
$end_date  = Input::get('end_date');
$data['contributions'] = Contribution:: getRanking($from_date, $end_date)->paginate(20);

return View::make('hoge', $data);
hoge.blade.php
{* ページネーション部分のみ抜粋 *}
{{$contributions->links()}}

この場合、下記のURLでアクセスしてみると、 1ページ目は正常に表示されるはずです。
http://hoge.com/contributions?from_date=xxxx-xx-xx&end_date=xxxx-xx-xx

しかし、2ページ目のリンクをクリックすると、from_dateとend_dateが無くなってしまいます。。
http://hoge.com/contributions?page=2

その場合は下記のように変更するとうまくいきます。

HogeController.php
<?php
...
$from_date = Input::get('from_date');
$end_date  = Input::get('end_date');
$data['contributions'] = Contribution::getRanking($from_date, $end_date)->paginate(20);
$data['params'] = array(
                            'from_date' => $from_date,
                            'end_date'  => $end_date
                        );

return View::make('hoge', $data);
hoge.blade.php
{* appendsにarrayで追加したいパラメータを渡します *}
{{$contributions->appends($params)->links()}}

これで無事2ページ目以降、下記のようなURLになったと思います。
http://hoge.com/contributions?from_date=xxxx-xx-xx&end_date=xxxx-xx-xx&page=2

42
52
1

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
42
52