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, 2020 in Laravel by kartik
• 37,510 points
22,013 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, 2020 by Niroj
• 82,880 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, 2020 in Laravel by Niroj
• 82,880 points
3,895 views
+2 votes
2 answers

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

You need do little modification in your ...READ MORE

answered Dec 10, 2020 in Laravel by anonymous
• 82,880 points
119,965 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, 2020 in Laravel by Niroj
• 82,880 points
16,183 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, 2020 in Laravel by Niroj
• 82,880 points
3,281 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,881 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,682 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,544 views
+1 vote
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, 2020 in Laravel by Niroj
• 82,880 points
41,735 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, 2020 in Laravel by Niroj
• 82,880 points
2,766 views
+1 vote
5 answers

How to get user's IP address in Laravel?

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

answered Mar 20, 2020 in Laravel by Niroj
• 82,880 points
71,095 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP