Beusebiu
Beusebiu

Reputation: 1513

Get unique value from table Laravel

I want to get all rows that have one unique column. The problem with my code is that it works only when I select just that one column, but in the end I need to have multiple columns.

$values= Model::select('id','value_first','gender_first','value_second','gender_second')->groupBy('value_first')->get();

Upvotes: 0

Views: 1931

Answers (2)

Naeem Ijaz
Naeem Ijaz

Reputation: 825

Here is Solution of Your Problem

$values= Model::select('id','value_first','gender_first','value_second','gender_second')->distinct('value_first')->get();

Upvotes: 2

Sehdev
Sehdev

Reputation: 5662

As per Laravel Documentation you can use distinct method for the same

The distinct method allows you to force the query to return distinct results for e.g

$values= Model::select('id','value_first','gender_first','value_second','gender_second')->distinct('value_first')->get();

Reference: Laravel-> Database: Query Builder -> Selects

Upvotes: 1

Related Questions