diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 4ba71af8610a..0eef56bede69 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -313,12 +313,15 @@ public function filter(callable $callback = null) * * @param bool $value * @param callable $callback + * @param callable $default * @return mixed */ - public function when($value, callable $callback) + public function when($value, callable $callback, callable $default = null) { if ($value) { return $callback($this); + } elseif ($default) { + return $default($this); } return $this; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 60e4c21f3995..a99d2b1477a9 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1914,6 +1914,19 @@ public function testWhen() $this->assertSame(['michael', 'tom'], $collection->toArray()); } + + public function testWhenDefault() + { + $collection = new Collection(['michael', 'tom']); + + $collection->when(false, function ($collection) { + return $collection->push('adam'); + }, function ($collection) { + return $collection->push('taylor'); + }); + + $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); + } } class TestSupportCollectionHigherOrderItem