In this article, we will see the laravel orderBy, groupBy, and limit examples. Here we will see different types of laravel 6/7/8 query examples. You can use the order by, group by, and limit functions for getting records from the database.
So, let's see the query of orderBy in laravel 7 and laravel 8, the query of the group by in laravel 7/8, and get a limited no of records in laravel 7/8.
The orderBy method allows you to sort the results of the query by a given column. The first argument accepted by the orderBy method should be the column you wish to sort by, while the second argument determines the direction of the sort and may be either asc or desc.
Laravel Example:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
SQL Query:
select * from `users` order by `name` desc;
Output:
All user's names will be sorted in descending order.
The groupBy
and having
methods may be used to group the query results. The having
method's signature is similar to that of the where
method.
$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();
The limit method is used to limit the number of results returned from the query.
$users = DB::table('users')
->limit(5)
->get();
In this example we will see laravel 8 order by query example. how to use order by in laravel 8.The orderBy met...
Dec-01-2021
In this article, we will see an example of laravel 8 custom email verification. Many web applications require users...
Dec-29-2021
In this article, we will see how to create custom middleware in laravel 9. Laravel middleware provides a conve...
Mar-27-2022
In this tutorial, I will explain you to how to get the selected checkbox value from a checkbox list in jquery, If y...
Jun-17-2020