@jasen_gottlieb
To paginate and sort results in Laravel, you can follow these steps:
1
|
$users = User::paginate(10); |
This will return a paginated collection of users with 10 users per page.
1
|
$users = User::orderBy('name', 'desc')->get(); |
This will return a collection of users sorted by their name in descending order.
Combining pagination and sorting:
To paginate and sort results at the same time, you can chain the orderBy()
method before the paginate()
method. For example, to paginate a list of users sorted by their name in ascending order with 10 users per page, you can use the following code:
1
|
$users = User::orderBy('name', 'asc')->paginate(10); |
This will return a paginated collection of users sorted by their name in ascending order with 10 users per page.
By following these steps, you can easily paginate and sort results in Laravel.