?
Brief introduction
In other frameworks, paging is a very painful thing, and Laravel makes it simple and easy to get started with. Laravel's paging is integrated with the Query builder and eloquent ORM, and provides a convenient, easy-to-use, database-based result set of paging in the Open box. The HTML-compatible bootstrap CSS framework generated by the page splitter.
Basic use of Query Builder-based paging
There are several ways to implement paging, and the simplest way is to use the method provided by the Query Builder or eloquent query paginate
. This method automatically sets the appropriate offsets (offset) and limits (limit) based on the current User View page, which is the page numbers and the number of displays per page. By default, the current page page
is judged by the value of the HTTP request query string parameter. Of course, this value is automatically detected by Laravel and then automatically inserted into the link generated by the pager.
Let's take a look at how to invoke a method in a query paginate
. In this case, the paginate
only parameter passed is the number you want to display per page, where we specify each page to show 15
:
<?phpnamespace app\http\controllers;use Illuminate\support\facades\db;use App\http\controllers\controller; Class Usercontroller extends controller{ /** * Show all users in the app * * @return Response * * Public function index () { $users = db::table (' users ')->paginate (); Return view (' User.index ', [' users ' = ' $users]);} }
Note: Currently, groupBy
the paging operation used cannot be performed effectively by Laravel, and if you need to use it in pagination results, it is groupBy
recommended that you manually query the database and then create the paging device.
Easy Paging
If you simply want to display the next page and previous page links in paginated view, you can use simplePaginate
methods to perform a more efficient query. This is useful when rendering views that contain large datasets and do not need to display each page number:
$users = db::table (' users ')->simplepaginate (15);
Paging based on the eloquent result set
You can also page through the results of the eloquent query, in this case, we User
paged the model, and each page shows a 15
record. As you can see, this syntax is similar to paging based on the query Builder:
$users = App\user::p aginate (15);
Of course, you can call after setting other constraints paginate
, such as where
clauses:
$users = User::where (' votes ', ' > ', '->paginate ') (15);
You can also use the method when paging the eloquent model simplePaginate
:
$users = User::where (' votes ', ' > ', '->simplepaginate ') (15);
To create a page splitter manually
Sometimes you might want to create a paging instance manually by passing array data, which you can create or instantiate based on your own needs Illuminate\Pagination\Paginator
Illuminate\Pagination\LengthAwarePaginator
.
Paginator
Class does not need to know the total number of data items in the result set, but because of this, the class also does not provide a way to get the last page index. LengthAwarePaginator
the receive parameter Paginator
is almost the same as the only difference in that it requires the total number of incoming result sets.
In other words, the corresponding method Paginator
simplePaginate
, and the LengthAwarePaginator
corresponding paginate
method.
Note: When creating a pager instance manually, you should manually "slice" The result set passed to the pager, and if you are unsure what to do, look at the PHP function Array_slice.
Show paged Results
When the paginate
method is called, you get the Illuminate\Pagination\LengthAwarePaginator
instance, and when the method is called, simplePaginate
it gets the Illuminate\Pagination\Paginator
instance. These objects provide methods that describe these result sets, in addition to these auxiliary functions, where the pager instance itself is an iterator that can be called as an array. So, after you get the results, you can use Blade to display the results and render the page links as follows:
<div class= "Container" > @foreach ($users as $user) {{$user->name}} @endforeach </div>{{$ Users->links ()}}
links
Method will render the other page links in the result set. Each link already contains a page
query string variable. Remember, render
the method generates HTML-compatible Bootstrap CSS frames.
Custom Paging Links
withPath
The method allows you to create a page link when you customize the URI used by the pager, for example, if you want the pager to generate http://example.com/custom/url?page=N
a link to the shape, it should be passed custom/url
to the withPath
method:
Route::get (' Users ', function () { $users = app\user::p aginate (); $users->withpath (' Custom/url '); //});
Add a parameter to a paging link
You can use appends
methods to add query parameters to a paging link query string. For example, to add &sort=votes
to each paging link, it should be called as follows appends
:
{{$users->appends ([' sort ' = ' votes '])->links ()}}
You can use the method if you want to add a "hash fragment" to a paging link fragment
. For example, to add #foo
to the end of each paging link, call the fragment
method like this:
{{$users->fragment (' foo ')->links ()}}
Convert the results to JSON
The Laravel results class implements Illuminate\Contracts\Support\JsonableInterface
contracts and provides toJson
methods, so it is very easy to convert paging results to JSON. You can also go back to JSON by returning a pager instance from a route or controller action:
Route::get (' Users ', function () { return App\user::p aginate ();});
The JSON converted from the pager contains meta information such as,,, and total
current_page
last_page
so on, and the actual result object data can be accessed through the keys in the JSON array data
. The following is a JSON example created from a pager instance returned from a route:
{ "total": Per_page, "current_page": 1, " last_page": 4, "First_page_url": "/HTTP// Laravel.app?page=1 ", " Last_page_url ":" Http://laravel.app?page=4 ", " Next_page_url ":" Http://laravel.app? " page=2 ", " Prev_page_url ": null, " path ":" Http://laravel.app ", " from ": 1, " to ":" Data ": [ { //Result Object }, { //Result Object } ]}
Customizing the Paging View
By default, the view used to render paging links is compatible with the Bootstrap CSS framework, and if you are not using Bootstrap, you can customize the view to render the links. When invoking a method on a pager instance links
, pass the view name as the first parameter:
{{$paginator->links (' view.name ')}}//pass data to view ... {{$paginator->links (' View.name ', [' foo ' = ' Bar ']}}}
However, the simplest way to customize the paging view is vendor:publish
to export the view file to the directory using the command resources/views/vendor
:
PHP Artisan vendor:publish--tag=laravel-pagination
The command places the view in the resources/views/vendor/pagination
directory, where the default.blade.php
file corresponds to the default view file, and editing the file modifies the paging HTML.
instance method of the page splitter
Each instance of a pager can provide additional paging information in the following ways:
$results->count () $results->currentpage () $results->firstitem () $results->hasmorepages () $results LastItem () $results->lastpage () (invalid with simplepaginate) $results->nextpageurl () $results->perpage () $ Results->previouspageurl () $results->total () (invalid with simplepaginate) $results->url ($page)
[Laravel 5.5 documentation] Database operations-Easy paging in Laravel