Pagination in Laravel is very easy, just use the paginate
method. This method can accept an integer parameter, indicating the number of records displayed on each page.
The steps are as follows:
Model::paginate()
method to obtain query results with pagination. use App\Models\User; public function index() { $users = User::orderBy('id', 'asc')->paginate(10); return view('users.index', ['users' => $users]); }
$users
variable in the view to render the paginator. <table class="table"> ... </table> {!! $users->links() !!}
The links()
method is used here to generate paging links. You can also manually set the style and layout of the paginator.
The style and layout of the paginator can also be customized. For example, you can use the simplePaginate()
method to create a paginator with only previous and next page links:
$users = User::orderBy('id', 'asc')->simplePaginate(10);
If you want to add custom parameters to the paginator, you can use appends
Method. For example, you can add search keywords and sorting methods:
$users = User::orderBy('id', 'asc')->paginate(10); $users->appends(['keyword' => 'xxx', 'orderBy' => 'id']);
Summary:
Laravel provides a very convenient paging function, which can be easily implemented using the paginate
method , use the links
method in the view to render the pager. At the same time, the paginator also supports custom parameters and styles.
The above is the detailed content of How to implement paging function in laravel (steps). For more information, please follow other related articles on the PHP Chinese website!