Laravel orderBy, groupBy and limit Example

Websolutionstuff | Jan-27-2021 | Categories : Laravel PHP MySQL

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 orderBy() Function

Laravel Example:

‚Äč$users = DB::table('users')
         ->orderBy('name', 'desc')

SQL Query:

select * from `users` order by `name` desc;


All user's names will be sorted in descending order.


Laravel groupBy() Function

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')
         ->having('account_id', '>', 100)


Laravel limit() Function

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

$users = DB::table('users')


Recommended Post
Featured Post
Mail: Laravel 11 Send Email using Queue
Mail: Laravel 11 Send Email us...

In this guide, we'll see how to send email using a queue in laravel 11. Here we'll see the concept of queue...

Read More


How To Validate Username And Password In React JS
How To Validate Username And P...

In this article, we will see how to validate username and password in react js. In this example, we will validate t...

Read More


Import Export CSV/EXCEL File In Laravel
Import Export CSV/EXCEL File I...

Today I will show you how to implement/install the import/export package in laravel 6/7. We will simply create...

Read More


How To Implement Google Bar Chart In Vue Js
How To Implement Google Bar Ch...

In this tutorial, we will see how to implement google bar chart in vue js. In vue js perform bar chart tutorial we are u...

Read More