Skip to content

Commit 78b5b92

Browse files
committed
add whereInstanceOfMethod
1 parent 85247ee commit 78b5b92

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Illuminate/Support/Collection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,19 @@ public function whereNotInStrict($key, $values)
610610
return $this->whereNotIn($key, $values, true);
611611
}
612612

613+
/**
614+
* Filter the items, removing any items that don't match the given type.
615+
*
616+
* @param string $type
617+
* @return static
618+
*/
619+
public function whereInstanceOf($type)
620+
{
621+
return $this->filter(function ($value) use ($type) {
622+
return $value instanceof $type;
623+
});
624+
}
625+
613626
/**
614627
* Get the first item from the collection.
615628
*

tests/Support/SupportCollectionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ public function testWhereStrict()
495495
);
496496
}
497497

498+
public function testWhereInstanceOf()
499+
{
500+
$c = new Collection([new stdClass, new stdClass, new Collection, new stdClass]);
501+
$this->assertCount(3, $c->whereInstanceOf(stdClass::class));
502+
}
503+
498504
public function testWhereIn()
499505
{
500506
$c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);

0 commit comments

Comments
 (0)