本文整理汇总了PHP中Illuminate\Support\Collection::groupBy方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::groupBy方法的具体用法?PHP Collection::groupBy怎么用?PHP Collection::groupBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Collection
的用法示例。
在下文中一共展示了Collection::groupBy方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: group
/**
* @param Collection $books
*
* @return array
*/
private function group($books)
{
$grouped = [];
foreach ($books->groupBy('id') as $episodes) {
foreach ($episodes as $key => $episode) {
$grouped[$key][] = $episode;
}
}
return $grouped;
}
示例2: authorizePermissionResource
/**
* Authorize a permission resource action based on the incoming request.
*
* @param string $resource
* @param array $options
* @return void
*/
public function authorizePermissionResource($resource, array $options = [])
{
$permissions = $this->resourcePermissionMap();
$collection = new Collection();
foreach ($permissions as $method => $ability) {
$collection->push(new Fluent(['ability' => $ability, 'method' => $method]));
}
$collection->groupBy('ability')->each(function ($permission, $ability) use($resource, $options) {
$this->middleware("can:{$resource}.{$ability}", $options)->only($permission->pluck('method')->toArray());
});
}
示例3: testGroupByClosureWhereItemsHaveMultipleGroupsPreservingKeys
public function testGroupByClosureWhereItemsHaveMultipleGroupsPreservingKeys()
{
$data = new Collection([10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']], 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']], 30 => ['user' => 3, 'roles' => ['Role_1']]]);
$result = $data->groupBy(function ($item) {
return $item['roles'];
}, true);
$expected_result = ['Role_1' => [10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']], 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']], 30 => ['user' => 3, 'roles' => ['Role_1']]], 'Role_2' => [20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']]], 'Role_3' => [10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']]]];
$this->assertEquals($expected_result, $result->toArray());
}
示例4: groupByDateFormat
protected function groupByDateFormat(Collection $backups, string $dateFormat) : Collection
{
return $backups->groupBy(function (Backup $backup) use($dateFormat) {
return $backup->date()->format($dateFormat);
});
}