Skip to content

Commit 75827f9

Browse files
BSN4taylorotwell
authored andcommitted
[6.x] Fix eloquent collection intersect (#30652)
* fix bug #30626 * Update Collection.php
1 parent 9beceac commit 75827f9

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Illuminate/Database/Eloquent/Collection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ public function intersect($items)
315315
{
316316
$intersect = new static;
317317

318+
if (empty($items)) {
319+
return $intersect;
320+
}
321+
318322
$dictionary = $this->getDictionary($items);
319323

320324
foreach ($this->items as $item) {

tests/Database/DatabaseEloquentCollectionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,22 @@ public function testCollectionReturnsDuplicateBasedOnlyOnKeys()
267267
$this->assertSame([1 => $two, 2 => $three], $duplicates);
268268
}
269269

270+
public function testCollectionIntersectWithNull()
271+
{
272+
$one = m::mock(Model::class);
273+
$one->shouldReceive('getKey')->andReturn(1);
274+
275+
$two = m::mock(Model::class);
276+
$two->shouldReceive('getKey')->andReturn(2);
277+
278+
$three = m::mock(Model::class);
279+
$three->shouldReceive('getKey')->andReturn(3);
280+
281+
$c1 = new Collection([$one, $two, $three]);
282+
283+
$this->assertEquals([], $c1->intersect(null)->all());
284+
}
285+
270286
public function testCollectionIntersectsWithGivenCollection()
271287
{
272288
$one = m::mock(Model::class);

0 commit comments

Comments
 (0)