Bijay Das logo
HomeBlogKeep2 (Beta)ContactSchedule a call

Connect me

  • Github
  • X (Twitter)
  • Linkedin
  • Telegram
  • Discord

Legal

  • Privacy policy
  • Terms of service

© Bijay Das 2025. All rights reserved.

We use cookies to give you the best possible experience on our website.

When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer. To find out more, read our updated.

  • Home/
  • Code

Struggled with detecting duplicate values within an array

Ever struggled with detecting duplicate values within an array? Laravel's 'distinct' validation rule provides a clean and efficient solution, eliminating the need for manual checks.

<?php

use Illuminate\Support\Facades\Validator;

$usernames = [ ['username' => 'tonystark'], ['username' => 'brucebanner'], ['username' => 'thor'], ['username' => 'tonystark'], // Duplicate value ];

// Instead of this $filteredCount = collect($usernames)->unique('username'); $result = $filteredCount->count() !== collect($usernames)->count();

// Use this Validator::make($usernames, ['*.username' => 'distinct:strict'])->fails();