Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,39 @@ public function zip($items)
return $this->toBase()->zip(...func_get_args());
}

/**
* Retrieve duplicate items from the collection.
*
* @param (callable(TModel): mixed)|string|null $callback
* @param bool $strict
* @return \Illuminate\Support\Collection<array-key, mixed>|static
*/
#[\Override]
public function duplicates($callback = null, $strict = false)
{
if (! is_null($callback)) {
return $this->toBase()->duplicates($callback, $strict);
}

return parent::duplicates($callback, $strict);
}

/**
* Retrieve duplicate items from the collection using strict comparison.
*
* @param (callable(TModel): mixed)|string|null $callback
* @return \Illuminate\Support\Collection<array-key, mixed>|static
*/
#[\Override]
public function duplicatesStrict($callback = null)
{
if (! is_null($callback)) {
return $this->toBase()->duplicatesStrict($callback);
}

return parent::duplicatesStrict($callback);
}

/**
* Get the comparison function to detect duplicates.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,23 @@ public function testCollectionReturnsDuplicateBasedOnlyOnKeys()
$this->assertSame([1 => $two, 2 => $three], $duplicates);
}

public function testCollectionDuplicatesWithKey()
{
$one = new TestEloquentCollectionModel;
$two = new TestEloquentCollectionModel;
$three = new TestEloquentCollectionModel;

$one->someAttribute = '1';
$two->someAttribute = '2';
$three->someAttribute = '1';

$duplicates = Collection::make([$one, $two, $three])->duplicates('someAttribute')->all();
$this->assertSame([2 => '1'], $duplicates);

$duplicates = Collection::make([$one, $two, $three])->duplicatesStrict('someAttribute')->all();
$this->assertSame([2 => '1'], $duplicates);
}

public function testCollectionIntersectWithNull()
{
$one = Mockery::mock(Model::class);
Expand Down