To sort records in Laravel, you can use the orderBy() method of the query builder. Here's an example:
$users = DB::table('users')
->orderBy('name', 'asc')
->get();
In this example, we build a query using the Laravel query builder that selects all users from the users table and orders the results by the name column in ascending order. The asc parameter specifies that the results should be sorted in ascending order.
If you want to sort the results in reverse order, you can use the desc parameter instead:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
In this example, the results are sorted in reverse order based on the name column.
You can also sort by multiple columns by calling the orderBy() method multiple times:
$users = DB::table('users')
->orderBy('name', 'asc')
->orderBy('email', 'asc')
->get();
In this example, the results are first sorted by the name column in ascending order, and then by the email column in ascending order.
Note that the orderBy() method modifies the query builder instance and does not execute the query. To execute the query and retrieve the results, you need to call one of the query execution methods like get(), first(), or paginate ().