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

Flex's avatar
Level 4

How can count table column unique values in Laravel 5.6?

working with laravel 5.6 and mysql. I have following table name as projects

id  name    type  
1   tuna        bio
2   nhye    IT
3   hyuj        bio
4   tour        IT
5   ghyt        commerce

I need count type column values which are unique values. as an example in above table I need count values 3 because there are three different values like bio, IT and commerce. how can I do this?

0 likes
2 replies
vajid's avatar
Project::select('type')->get()->groupBy('type')
    ->map(function($projectsType){
        return $projectsType->count();
    }
);


realrandyallen's avatar
$projectCount = DB::table('projects')->select('type')->distinct()->get()->count();
1 like

Please or to participate in this conversation.