Count Duplicated Items in Laravel Collections Example

Sovary November 30, 2022 639
2 minutes read

Today we will show how to count duplicated value by the article below. We will using function Laravel Collections. We will count elements where duplication exist. Using Illuminate\Support\Collection class provide convenient wrapper for working with collection data. You also can use the method in Laravel 6, Laravel 7, Laravel 8, and Laravel 9. 


Let's see example below how to count duplicated item in Laravel Collections.

Count Duplicated Item in Collections

  • Step 1 - Install Laravel
  • Step 2 - Create Controller
  • Step 3 - Add Route
  • Step 4 - Start Laravel Server

Step 1 - Install Laravel

First we have to download Laravel project in your local machine. Open the terminal and run following command

composer create-project laravel/laravel Blog

Step 2 - Create Controller

Now this step we will create new controller file to implement some code in a function. Type the following command to create file.

php artisan make:controller TestController

Open file app -> Http -> Controllers -> TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    
    public function index()
    {
        $items = collect([3,6,6,2,1,6,3]);
        $value = $items ->countBy();
        dd($value);
    }

    public function indexByKey()
    {
        $items = collect([
         ["name" => "Sok", "gender" => "M"],
         ["name" => "Makara", "gender" => "M"],
         ["name" => "Nika", "gender" => "F"],
        ]);
        $value = $items ->countBy(function($value)
        {
           return $value["gender"];
        });
        dd($value);
    }

}

Step 3 - Add Route

You will need to add route where your controller file located and the function should call. As we have two functions then we need two routes as well.

<?php
  
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\TestController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
Route::get('/', [TestController::class,'index']);
Route::get('/key',[TestController::class,'indexByKey']);

Step 4 - Start Laravel Server

This step we will run following command to start server and launch new session in browser with following URL http://localhost:8000/  and http://localhost:8000/key 

You will see the result as below

http://localhost:8000/ 

Illuminate\Support\Collection {#1897 ▼
  #items: array:4 [▼
    3 => 2
    6 => 3
    2 => 1
    1 => 1
  ]
}

http://localhost:8000/key 

Illuminate\Support\Collection {#1916 ▼
  #items: array:2 [▼
    "M" => 2
    "F" => 1
  ]
}

Hope this would help you to count duplicated item in collections Larave. Have a nice day!!!

You may also like...

Laravel  PHP  Laravel 9  Laravel Collections 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him