9
9

More than 5 years have passed since last update.

Laravel4でページネーションとbootsrap3への対応

Posted at

PHPフレームワークのLaravelにはあらかじめ、
ページネーションの機能が備わっているので簡単に使用する事ができます。
ただ、僕のインストールした段階では、
bootstrap3に対応していなかったので、少しハマったので、
その部分を合わせて説明したいと思います。

Viewにページネーションの値を渡す

今回は、routes.phpにからViewに渡してみます。
※DBの設定はあらかじめconfig/datebase.phpで行っておいてください。
※テンプレートエンジンでbladeを使用しています。

routes.php
//対象のURLはhttp//:hoge.com/testsとします。
Route::get('tests', function()
{
    $data = DB::table('カラム名') -> paginate(1ページに表示したいデータ数int型);
    //並び替えをしたい場合はorederByを使用する。
    $data = DB::table('カラム名') -> orderBy('列名', 'descもしくはasc') ->paginate(1ページに表示したいデータ数int型);
    //例
    $data = DB::table('user') -> orderBy('id', 'desc') ->paginate(20);

    //viewファイルはtest.blade.php
    return View::make('test',array('data' => $data));
})

view側

test.blade.php
@foreach ($data as $value)
   //データを表示するコードを書く
@endforeach

//ページネーションを表示したい場所で
{{$data->links()}};

bootstrap3に対応していない場合

上記の手順でページネーションの機能が実装されますが、
表示が崩れている場合、bootstrap3に対応していない場合があります。

その場合は、/app/config/view.phpを書き換えます。

/app/config/view.php
   'pagination' => 'pagination::slider-3',

これで、bootstrap3に対応することができます。

9
9
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
9
9