How to get distinct values for non-key column fields in Laravel?

0 votes
Hello,

I have a table that can have repeated values for a particular non-key column field. How do I write a SQL query using Query Builder or Eloquent that will fetch rows with distinct values for that column?
Mar 30 in Laravel by kartik
• 11,290 points
578 views

1 answer to this question.

0 votes

Hello @kartik

In eloquent you can use this

$users = User::select('name')->groupBy('name')->get()->toArray() ;

groupBy is actually fetching the distinct values, in fact the groupBy will categorize the same values, so that we can use aggregate functions on them. but in this scenario we have no aggregate functions, we are just selecting the value which will cause the result to have distinct values.

Hope this will help you out!

Thank you!!

answered Mar 30 by Niroj
• 22,790 points

Related Questions In Laravel

0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24 in Laravel by Niroj
• 22,790 points
86 views
0 votes
1 answer

How to add a new column to existing table of laravel in a migration?

Hii @kartik, To create a migration, you may ...READ MORE

answered Mar 30 in Laravel by Niroj
• 22,790 points
543 views
0 votes
1 answer

How to set port for php artisan.php serve in Laravel?

Hii @kartik, When we use the php artisan serve ...READ MORE

answered Mar 30 in Laravel by Niroj
• 22,790 points
154 views
0 votes
1 answer

How to get a list of registered route paths in Laravel?

Hello, Route::getRoutes() returns a RouteCollection. On each element, you can ...READ MORE

answered Mar 31 in Laravel by Niroj
• 22,790 points
113 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 22,790 points
434 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 22,790 points
62 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 22,790 points
64 views
0 votes
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 22,790 points
151 views
0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19 in Laravel by Niroj
• 22,790 points
40 views
0 votes
1 answer

How to get user's IP address in Laravel?

Hey, We can get the user's IP address ...READ MORE

answered Mar 20 in Laravel by Niroj
• 22,790 points
370 views