group() public method

Groups the collection by a given callback
public group ( callable $callback ) : object
$callback callable
return object A new collection with an item for each group and a subcollection in each group
Example #1
0
 public function testGroup()
 {
     $collection = new Collection();
     $collection->user1 = array('username' => 'peter', 'group' => 'admin');
     $collection->user2 = array('username' => 'paul', 'group' => 'admin');
     $collection->user3 = array('username' => 'mary', 'group' => 'client');
     $groups = $collection->group(function ($item) {
         return $item['group'];
     });
     $this->assertEquals(2, $groups->admin()->count());
     $this->assertEquals(1, $groups->client()->count());
     $firstAdmin = $groups->admin()->first();
     $this->assertEquals('peter', $firstAdmin['username']);
 }
Example #2
0
 public static function __callStatic($name, $arguments)
 {
     $collection = new Collection($arguments[0]);
     switch ($name) {
         case '_columns':
             return $collection->columns($arguments[1]);
         case '_where':
             return $collection->where($arguments[1]);
         case '_join':
             return $collection->join($arguments[1]);
         case '_order':
             return $collection->order($arguments[1], $arguments[2]);
         case '_group':
             return $collection->group($arguments[1]);
         case '_limit':
             return $collection->limit($arguments[1]);
         case '_offset':
             return $collection->offset($arguments[1]);
         case '_all':
             return $collection->all();
         default:
             throw new \Bacon\Exceptions\MethodNotFound();
     }
 }