diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 4a879bd36673..498b1b4bd280 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -624,6 +624,17 @@ public function intersect($items) return new static(array_intersect($this->items, $this->getArrayableItems($items))); } + /** + * Intersect the collection with the given items by key. + * + * @param mixed $items + * @return static + */ + public function intersectKey($items) + { + return new static(array_intersect_key($this->items, $this->getArrayableItems($items))); + } + /** * Determine if the collection is empty or not. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index a05adfa1f16f..15f5a40e6860 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -564,6 +564,18 @@ public function testIntersectCollection() $this->assertEquals(['first_word' => 'Hello'], $c->intersect(new Collection(['first_world' => 'Hello', 'last_word' => 'World']))->all()); } + public function testIntersectKeyNull() + { + $c = new Collection(['id' => 1, 'first_word' => 'Hello']); + $this->assertEquals([], $c->intersectKey(null)->all()); + } + + public function testIntersectKeyCollection() + { + $c = new Collection(['id' => 1, 'first_word' => 'Hello']); + $this->assertEquals(['first_word' => 'Hello'], $c->intersectKey(new Collection(['first_word' => 'Hello', 'last_word' => 'World']))->all()); + } + public function testUnique() { $c = new Collection(['Hello', 'World', 'World']);