Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shawdow's avatar

writting distinct query example

i need show the unique values for whole table i have searched in google the query written was

DB::table('products')->select('color')->distinct()->get();

the above query displays the unique color value

how to query for whole table (correct me if i am wrong)

DB::table(products')->select('*')->distinct()->get();
0 likes
10 replies
Shawdow's avatar

@bobbybouwmann i have tried with below query it displays the unique value of colors

DB::table('products')->select('color')->distinct()->get();

other query like below won't displays the unique value

DB::table('products')->select('*')->distinct()->get();
bobbybouwmann's avatar

Probably because you don't specify which column should be unique here. I think Laravel doesn't support this and you need to write a raw query for it!

Shawdow's avatar

@bobbybouwmann not getting to write raw query using above link!! there is no example for using distinct clause!!

bobbybouwmann's avatar
Level 88

Well distinct in a sql keyword. You should write a normal SQL query and perform that on your database

Please or to participate in this conversation.