Securing PHP APIs with Laravel Passport
When a PHP API serves multiple clients, Laravel Passport lets those clients authenticate and receive limited permissions through OAuth 2.0. OAuth 2.0…
When you inspect or clean data in a Laravel collection, choose a method based on what you want back. unique() keeps the first item for each value. duplicates() and duplicatesStrict() return later items whose values repeat. A Laravel collection wraps an array and provides methods for working with its items.
The main choice is how Laravel compares values. Loose comparison can treat different types as equal, such as the integer 1 and the string '1'. Strict comparison requires the values and their types to match. Use loose comparison when PHP’s type conversion matches your rule. Use strict comparison when the type matters.
unique()Call unique() to remove later items that compare equal to an earlier item. With no argument, Laravel compares the collection’s values. To compare a value within each item, pass a field name or a callback function that Laravel runs for each item. Laravel’s collection documentation says unique() retains unique items. The framework implementation shows that it keeps the first item it encounters for each value it compares.
$values = collect([1, '1', 1]);
$values->unique()->all();
// [0 => 1]
$values->uniqueStrict()->all();
// [0 => 1, 1 => '1']
By default, unique() uses loose comparison, so it treats the integer and numeric string as equal in this example. The later integer also matches the first integer. uniqueStrict() keeps the string because it has a different type, then removes the later integer.
Both methods preserve the original keys of the items they keep. If you need a zero-based list afterward, call values():
$values->unique()->values()->all();
// [1]
For records, pass the field you want Laravel to compare. unique('email') compares each record’s email value, keeps the first record for each value, and returns those records.
duplicates()Use duplicates() to find repeated values without changing the original collection. It skips the first occurrence of each value and returns later matches with their original keys. For example:
$values = collect(['a', 'b', 'a', 'c', 'b']);
$values->duplicates()->all();
// [2 => 'a', 4 => 'b']
Each key identifies a repeated item’s position in the original collection. Use duplicates() to report or review repeats while keeping the original collection available.
You can also compare a field in each record:
$employees = collect([
['email' => 'abigail@example.com', 'position' => 'Developer'],
['email' => 'james@example.com', 'position' => 'Designer'],
['email' => 'victoria@example.com', 'position' => 'Developer'],
]);
$duplicates = $employees->duplicates('position')->all();
// [2 => 'Developer']
Here, the result contains the repeated position value, not the full employee record. To get the full records, use the returned keys with the original collection:
$duplicateEmployees = $employees
->only(array_keys($duplicates))
->all();
// [2 => ['email' => 'victoria@example.com', 'position' => 'Developer']]
duplicatesStrict() when types matterLike duplicates(), duplicatesStrict() reports a value only when an earlier value has the same type and value. Laravel’s implementation passes strict comparison to the duplicate check. The PHP comparison rules distinguish loose equality (==), which can treat different types as equal, from strict identity (===), which requires the type and value to match.
$values = collect([1, '1', 1]);
$values->duplicates()->all();
// [1 => '1', 2 => 1]
$values->duplicatesStrict()->all();
// [2 => 1]
With loose comparison, the string '1' matches the first integer 1, so Laravel reports both later items. With strict comparison, the string’s type differs, so only the final integer is a duplicate.
Strict comparison uses text as written: uppercase and lowercase strings remain different, and surrounding whitespace remains part of the comparison. If your rule should ignore those differences, pass a callback function that prepares each comparison value:
$duplicates = $employees->duplicates(
fn (array $employee) => strtolower(trim($employee['email']))
);
The callback trims surrounding whitespace from email values and lowercases their letters. Use this transformation only if it matches your data rules.
Use unique() to keep the first item for each value under loose comparison. Use duplicates() to find later repeats under the same rule. If values with different types must stay distinct, use strict comparison: uniqueStrict() keeps the first item for each value, and duplicatesStrict() reports later repeats.
PHP’s loose comparison rules depend on the values being compared, and those rules have changed across PHP versions. If your code compares numeric-looking strings or mixed types, check the rules for the PHP version your Laravel project uses. The Laravel documentation and source cited here cover Laravel 13.x. If your project uses another major version, check its matching documentation and source.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com