設定Configuration
他のフレームワークのペジネーションは苦痛に満ちています。Laravelはこれを簡単にしてくれます。Laravelは現在のページ位置に基づいて、賢い「ページ範囲」のリンクを生成します。生成されるHTMLはBootstrap CSSフレームワークへ適応しています。In other frameworks, pagination can be very painful. Laravel makes it a breeze. Laravel can generate an intelligent "range" of links based on the current page. The generated HTML is compatible with the Bootstrap CSS framework.
使用法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
を使用したペジネーションを使用する必要がある場合は、データベースクエリーを実行し、その結果を元にペジネーターを自前で作成してください。Note: Currently, pagination operations that use agroupBy
statement cannot be executed efficiently by Laravel. If you need to use agroupBy
with a paginated result set, it is recommended that you query the database and create a paginator manually.
ペジネーターの作成Creating A Paginator Manually
渡された配列を元にして、ペジネーションインスンタンスを作成したいこともあります。必要に応じてIlluminate\Pagination\Paginator
か、Illuminate\Pagination\LengthAwarePaginator
インスタンスを生成することで実現できます。Sometimes you may wish to create
a pagination instance manually, passing it an array
of items. You may do so by creating either an
Illuminate\Pagination\Paginator
or
Illuminate\Pagination\LengthAwarePaginator
instance, depending on your needs.
EloquentモデルのペジネーションPaginating An Eloquent Model
さらにEloquentモデルもペジネーションできます。You may also paginate Eloquent[/docs/{{version}}/eloquent] models:
$allUsers = User::paginate(15);
$someUsers = User::where('votes', '>', 100)->paginate(15);
paginate
メソッドに渡している引数は表示したいアイテムの個数です。一度結果を受け取ったら、それをビューで表示してください。それからrender
メソッドを使ってペジネーションリンクを生成してください。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
render
method:
<div class="container">
<?php foreach ($users as $user): ?>
<?php echo $user->name; ?>
<?php endforeach; ?>
</div>
<?php echo $users->render(); ?>
こうした生成は全部ペジネーションシステムが面倒を見ます。フレームワークに現在ページを指定していないことに注意してください。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.
さらに、以下のメソッドを使用し、追加のペジネーション情報にアクセスできます。You may also access additional pagination information via the following methods:
currentPage
currentPage
lastPage
lastPage
perPage
perPage
hasMorePages
hasMorePages
url
url
nextPageUrl
nextPageUrl
firstItem
firstItem
lastItem
lastItem
total
total
count
count
シンプル・ペジネーション"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);
ペジネーターでのカスタムURI使用Customizing The Paginator URI
ペジネーターにより使用されるURIも、setPath
メソッドでカスタマイズできます。You
may also
customize
the URI used
by the
paginator
via the
setPath
method:
$users = User::paginate();
$users->setPath('custom/url');
上の例では、http://example.com/custom/url?page=2
のURLリンクが生成されます。The
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(['sort' => 'votes'])->render(); ?>
この例では、以下のような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')->render(); ?>
このメソッドの呼び出しで、次のような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\Contracts\Support\JsonableInterface
契約を実装しており、toJson
メソッドを備えています。Paginator
インスタンスをルートから返すことにより、JSONに変換することもできます。インスタンスのJSON形式には、total
やcurrent_page
、last_page
のような「メタ」情報が含まれています。インスタンスのデーターは、JSON配列のdata
キーにより利用できます。The
Paginator
class
implements
the
Illuminate\Contracts\Support\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
,
and
last_page
.
The
instance's
data will be
available
via the
data
key in the
JSON
array.