diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index bd19409e1d10..ac8f991a9e3c 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -1899,14 +1899,14 @@ public function count() public function countBy($predicate = null) { if (is_null($predicate)) { - $predicate = function ($val, $key) { + $predicate = function ($val) { return $val; }; } return new static( $this->groupBy($predicate) - ->map(function ($val, $key) { + ->map(function ($val) { return $val->count(); }) ); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 0b21f1d0efa0..2b47481fa681 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -309,25 +309,25 @@ public function testCountable() public function testCountableByWithoutPredicate() { - $c = new Collection([ 'foo', 'foo', 'foo', 'bar', 'bar', 'foobar' ]); - $this->assertEquals([ 'foo' => 3, 'bar' => 2, 'foobar' => 1 ], $c->countBy()->all()); + $c = new Collection(['foo', 'foo', 'foo', 'bar', 'bar', 'foobar']); + $this->assertEquals(['foo' => 3, 'bar' => 2, 'foobar' => 1], $c->countBy()->all()); - $c = new Collection([ true, true, false, false, false ]); - $this->assertEquals([ true => 2, false => 3, ], $c->countBy()->all()); + $c = new Collection([true, true, false, false, false]); + $this->assertEquals([true => 2, false => 3,], $c->countBy()->all()); - $c = new Collection([ 1, 5, 1, 5, 5, 1 ]); - $this->assertEquals([ 1 => 3, 5 => 3, ], $c->countBy()->all()); + $c = new Collection([1, 5, 1, 5, 5, 1]); + $this->assertEquals([1 => 3, 5 => 3,], $c->countBy()->all()); } public function testCountableByWithPredicate() { - $c = new Collection([ 'alice', 'aaron', 'bob', 'carla' ]); - $this->assertEquals([ 'a' => 2, 'b' => 1, 'c' => 1 ], $c->countBy(function ($name) { + $c = new Collection(['alice', 'aaron', 'bob', 'carla']); + $this->assertEquals(['a' => 2, 'b' => 1, 'c' => 1], $c->countBy(function ($name) { return substr($name, 0, 1); })->all()); - $c = new Collection([ 1, 2, 3, 4, 5 ]); - $this->assertEquals([ true => 2, false => 3 ], $c->countBy(function ($i) { + $c = new Collection([1, 2, 3, 4, 5]); + $this->assertEquals([true => 2, false => 3], $c->countBy(function ($i) { return $i % 2 === 0; })->all()); }