DEV Community

Cover image for Laravel Collection Count and CountBy Method Example
Msh Sayket
Msh Sayket

Posted on

Laravel Collection Count and CountBy Method Example

In this tutorial. I am going to explain you example of laravel collection count example. you will learn laravel collection countby example. This tutorial will give you simple example of count collection laravel. you can see laravel collection count by key.

I will give you list of examples of count and countby colletion in laravel. so you can easily use it with your laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11, laravel 12 application. so let’s see bellow example that will helps you lot. You Can Learn How to send email using a Gmail account in laravel 12

Laravel Collection Count and CountBy Method Example

Count Syntax:

$collecton->count();
Enter fullscreen mode Exit fullscreen mode

Laravel Collection count() Example

public function index()
{    
$collection = collect([1, 2, 3, 4, 5, 6]);      
$count = $collection->count();           
dd($count);
}
Enter fullscreen mode Exit fullscreen mode

Output:

6
CountBy Syntax:

$collecton->countBy(    
$callback
);
Enter fullscreen mode Exit fullscreen mode

Laravel Collection countBy() Example

public function index(){    
$collection = collect(["one", "two", "two", "three", "three", "four"]);      
$count = $collection->countBy();           
dd($count);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Illuminate\Support\Collection Object(
    [items:protected] => Array        
 (            
 [one] => 1            
 [two] => 2            
 [three] => 2            
 [four] => 1        
 )
)
Enter fullscreen mode Exit fullscreen mode

Laravel Collection countBy() with function Example

public function index(){    
$collection = collect([            
["id"=>1, "name"=>"Hardik", "role"=>"Admin"],            
["id"=>2, "name"=>"Paresh", "role"=>"Admin"],            
["id"=>3, "name"=>"Rakesh", "role"=>"User"],        
]);      
$count = $collection->countBy(function ($item) {                   
 return $item['role'];                
});            
dd($count);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Illuminate\Support\Collection Object
(    
[items:protected] => Array        
 (           
 [Admin] => 2            
 [User] => 1        
 )
)
Enter fullscreen mode Exit fullscreen mode

I hope it can help you… Read More Tutorials from DevScriptSchool

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay