DEV Community

Websolutionstuff
Websolutionstuff

Posted on • Originally published at websolutionstuff.com

Laravel orderBy, groupBy and limit Example

In this example we will see laravel orderBy, groupBy and limit example, here we will see diffrent type of laravel 8 query example.

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.

orderBy()

Laravel Example :

​$users = DB::table('users')
         ->orderBy('name', 'desc')
         ->get();
Enter fullscreen mode Exit fullscreen mode

SQL Query:

select * from `users` order by `name` desc;
Enter fullscreen mode Exit fullscreen mode

Output :

All users name will be sort by descending order.

groupBy()

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();
Enter fullscreen mode Exit fullscreen mode

limit()

limit method is used to limit the number of results returned from the query.

$users = DB::table('users')
         ->limit(5)
         ->get();
Enter fullscreen mode Exit fullscreen mode

Read Also : Laravel 8 Create Custom Helper Function Example

Read Also : Laravel AJAX CRUD example

Read Also : How To Get Current User Location In Laravel

Thanks for the reading...!!
Don't forgot to like, share and comments.

Discussion (0)