diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index ba45f3b0676e..a20d39d5d894 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -610,6 +610,19 @@ public function whereNotInStrict($key, $values) return $this->whereNotIn($key, $values, true); } + /** + * Filter the items, removing any items that don't match the given type. + * + * @param string $type + * @return static + */ + public function whereInstanceOf($type) + { + return $this->filter(function ($value) use ($type) { + return $value instanceof $type; + }); + } + /** * Get the first item from the collection. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index b3c21537fb77..17a5a5697184 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -495,6 +495,12 @@ public function testWhereStrict() ); } + public function testWhereInstanceOf() + { + $c = new Collection([new stdClass, new stdClass, new Collection, new stdClass]); + $this->assertCount(3, $c->whereInstanceOf(stdClass::class)); + } + public function testWhereIn() { $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);