Skip to content

Commit

Permalink
Add countBy to collection docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jamierumbelow authored Mar 5, 2019
1 parent dac0f70 commit 9d8d25c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,29 @@ The `count` method returns the total number of items in the collection:

// 4

<a name="method-countBy"></a>
#### `countBy()` {#collection-method}

The `countBy` method counts the occurences of values in the collection.

By default, it counts the occurrences of every element:

$collection = collect([1, 2, 2, 2, 3]);

$collection->countBy();

// collect([1 => 1, 2 => 3, 3 => 1])

It also accepts a callback:

$collection = collect([ '[email protected]', '[email protected]', '[email protected]' ]);

$collection->countBy(function ($email) {
return substr(strrchr($email, "@"), 1);
});

// collect([ 'gmail.com' => 2, 'yahoo.fr' => 1 ])

<a name="method-crossjoin"></a>
#### `crossJoin()` {#collection-method}

Expand Down

0 comments on commit 9d8d25c

Please sign in to comment.