Readouble

Laravel 4.2 ペジネーション

設定Configuration

他のフレームワークではペジネーションは苦痛に満ちています。Laravelでは簡単です。app/config/view.phpに一つのオプションがあるだけです。paginationオプションはペジネーションリンクを作成するために、どのビューを使用するかを指定します。デフォルトで2つのビューをLaravelは持っています。In other frameworks, pagination can be very painful. Laravel makes it a breeze. There is a single configuration option in the app/config/view.php file. The pagination option specifies which view should be used to create pagination links. By default, Laravel includes two views.

pagination::sliderは現在のページに基づいて知的にリンクの「範囲」を表示します。pagination::simpleはシンプルに「前」と「次」ボタンを表示します。両方のビューは最初からTwitter Bootstrapとコンパチブルです。The pagination::slider view will show an intelligent "range" of links based on the current page, while the pagination::simple view will simply show "previous" and "next" buttons. Both views are compatible with Twitter Bootstrap out of the box.

使用法Usage

アイテムをページ分けするには多くの方法があります。一番シンプルな方法はEloquentモデルかクエリービルダーに、paginateメソッドを用いることです。There are several ways to paginate items. The simplest is by using the paginate method on the query builder or an Eloquent model.

データベースの結果をペジネーションするPaginating Database Results

$users = DB::table('users')->paginate(15);

注意: 現在、groupByメソッドを使用したペジネーション操作は、Laravelで正しく実行できません。groupByを使用したペジネーションを使用する必要がある場合は、データベースクエリーを実行し、その結果でPaginator::makeを使用することをおすすめします。Note: Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database manually and use Paginator::make.

EloquentモデルをペジネーションするPaginating An Eloquent Model

さらにEloquentモデルもペジネーションできます。You may also paginate Eloquent[/docs/4.2/eloquent] models:

$allUsers = User::paginate(15);

$someUsers = User::where('votes', '>', 100)->paginate(15);

paginateメソッドに渡している引数は表示したいアイテムの個数です。一度結果を受け取ったら、それをビューで表示してください。それからlinksメソッドを使ってペジネーションリンクを生成してください。The argument passed to the paginate method is the number of items you wish to display per page. Once you have retrieved the results, you may display them on your view, and create the pagination links using the links method:

<div class="container">
	<?php foreach ($users as $user): ?>
		<?php echo $user->name; ?>
	<?php endforeach; ?>
</div>

<?php echo $users->links(); ?>

これらの生成は全部ペジネーションシステムが面倒を見ます。フレームワークに現在ページを指定していないことに注意してください。Laravelは自動的に判断します。This is all it takes to create a pagination system! Note that we did not have to inform the framework of the current page. Laravel will determine this for you automatically.

ペジネーションでカスタムビューを指定したい場合は、linksメソッドでビューを指定して下さい。If you would like to specify a custom view to use for pagination, you may pass a view to the links method:

<?php echo $users->links('view.name'); ?>

さらに、以下のメソッドを使用し、追加のペジネーション情報にアクセスできます。You may also access additional pagination information via the following methods:

  • getCurrentPagegetCurrentPage
  • getLastPagegetLastPage
  • getPerPagegetPerPage
  • getTotalgetTotal
  • getFromgetFrom
  • getTogetTo
  • countcount

シンプル・ペジネーション"Simple Pagination"

「次」と「前」のリンクだけをペジネーションビューに表示したい場合は、simplePaginateメソッドを使用する選択肢により、より効率的にクエリーすべきでしょう。これは、ビューに正確なページ番号を表示する必要がない、巨大なデータセットに対して便利です。If you are only showing "Next" and "Previous" links in your pagination view, you have the option of using the simplePaginate method to perform a more efficient query. This is useful for larger datasets when you do not require the display of exact page numbers on your view:

$someUsers = User::where('votes', '>', 100)->simplePaginate(15);

ペジネーターを手動で生成するCreating A Paginator Manually

時には手動でペジネーションインスタンスを生成したい場合もあるでしょう。その場合はPaginator::makeを使います。Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so using the Paginator::make method:

$paginator = Paginator::make($items, $totalItems, $perPage);

ペジネーターでカスタムURIを使用するCustomizing The Paginator URI

ペジネーターにより使用されるURIも、setBaseUrlメソッドでカスタマイズできます。You may also customize the URI used by the paginator via the setBaseUrl method:

$users = User::paginate();

$users->setBaseUrl('custom/url');

上の例では、次のURLリンクが生成されます。http://example.com/custom/url?page=2The example above will create URLs like the following: http://example.com/custom/url?page=2[http://example.com/custom/url?page=2]

ペジネーションリンクの追加Appending To Pagination Links

ペジネーターのappendsメソッドを使用しペジネーションリンクのクエリー文字列を追加することができます。You can add to the query string of pagination links using the appends method on the Paginator:

<?php echo $users->appends(array('sort' => 'votes'))->links(); ?>

この例で、以下のようなURLが生成されます。This will generate URLs that look something like this:

http://example.com/something?page=2&sort=votes

ペジネーションのURLに「ハッシュフラグメント」を追加したい場合は、fragmentメソッドが使用できます。If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method:

<?php echo $users->fragment('foo')->links(); ?>

このメソッドの呼び出しで、次のようなURLが表示されます。This method call will generate URLs that look something like this:

http://example.com/something?page=2#foo

JSONへ変換するConverting To JSON

PaginatorクラスはIlluminate\Support\Contracts\JsonableInterface契約を実装しており、toJsonメソッドを提供しています。ルートからPaginatorインスタンスをリターンすれば、JSONに変換されます。インスタンスが変換された後のJSONには、totalcurrent_pagelast_pagefromtoのような「メタ」情報を含みます。インスタンスの情報はJSON配列のdataキーで利用できます。The Paginator class implements the Illuminate\Support\Contracts\JsonableInterface contract and exposes the toJson method. You may also convert a Paginator instance to JSON by returning it from a route. The JSON'd form of the instance will include some "meta" information such as total, current_page, last_page, from, and to. The instance's data will be available via the data key in the JSON array.

カスタム・プレゼンターCustom Presenters

初めからLaravelに用意されている、デフォルトのペジネーションプレゼンターは、Bootstrapと互換性があります。しかし、皆さんが選んだプレゼンテーターに合わせ、カスタマイズすることができます。The default pagination presenter is Bootstrap compatible out of the box; however, you may customize this with a presenter of your choice.

Abstractのプレゼンターを拡張するExtending The Abstract Presenter

Illuminate\Pagination\Presenterクラスを拡張し、抽象メソッドを実装してください。例えば、ZurbのFoundationでは、次のようになるでしょう。Extend the Illuminate\Pagination\Presenter class and implement its abstract methods. An example presenter for Zurb Foundation might look like this:

class ZurbPresenter extends Illuminate\Pagination\Presenter {

    public function getActivePageWrapper($text)
    {
        return '<li class="current"><a href="">'.$text.'</a></li>';
    }

    public function getDisabledTextWrapper($text)
    {
        return '<li class="unavailable"><a href="">'.$text.'</a></li>';
    }

    public function getPageLinkWrapper($url, $page, $rel = null)
    {
        return '<li><a href="'.$url.'">'.$page.'</a></li>';
    }

}

カスタムプレゼンターを使用するUsing The Custom Presenter

最初に、カスタムプレゼンターとして働くビューをapp/viewsディレクトリーの中に作成してください。次に、app/config/view.php設定ファイルの、paginationオプションへ新しいビューの名前を指定します。最後に、以下のコードをカスタムプレゼンタービューに、設置してください。First, create a view in your app/views directory that will serve as your custom presenter. Then, replace pagination option in the app/config/view.php configuration file with the new view's name. Finally, the following code would be placed in your custom presenter view:

<ul class="pagination">
    <?php echo with(new ZurbPresenter($paginator))->render(); ?>
</ul>

章選択

Artisan CLI

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作