Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Handle duplicates method in eloquent collections #28194

Merged
merged 1 commit into from Apr 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Expand Up @@ -305,6 +305,19 @@ public function diff($items)
return $diff;
}

/**
* Get the comparison function to detect duplicates.
*
* @param bool $strict
* @return \Closure
*/
protected function duplicateComparator($strict)
{
return function ($a, $b) {
return $a->is($b);
};
}

/**
* Intersect the collection with the given items.
*
Expand Down
26 changes: 20 additions & 6 deletions src/Illuminate/Support/Collection.php
Expand Up @@ -420,12 +420,7 @@ public function duplicates($callback = null, $strict = false)

$uniqueItems = $items->unique(null, $strict);

$compare = $strict ? function ($a, $b) {
return $a === $b;
}
: function ($a, $b) {
return $a == $b;
};
$compare = $this->duplicateComparator($strict);

$duplicates = new static;

Expand All @@ -451,6 +446,25 @@ public function duplicatesStrict($callback = null)
return $this->duplicates($callback, true);
}

/**
* Get the comparison function to detect duplicates.
*
* @param bool $strict
* @return \Closure
*/
protected function duplicateComparator($strict)
{
if ($strict) {
return function ($a, $b) {
return $a === $b;
};
}

return function ($a, $b) {
return $a == $b;
};
}

/**
* Execute a callback over each item.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Expand Up @@ -245,6 +245,28 @@ public function testCollectionDiffsWithGivenCollection()
$this->assertEquals(new Collection([$one]), $c1->diff($c2));
}

public function testCollectionReturnsDuplicateBasedOnlyOnKeys()
{
$one = new TestEloquentCollectionModel();
$two = new TestEloquentCollectionModel();
$three = new TestEloquentCollectionModel();
$four = new TestEloquentCollectionModel();
$one->id = 1;
$one->someAttribute = '1';
$two->id = 1;
$two->someAttribute = '2';
$three->id = 1;
$three->someAttribute = '3';
$four->id = 2;
$four->someAttribute = '4';

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

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

public function testCollectionIntersectsWithGivenCollection()
{
$one = m::mock(Model::class);
Expand Down