当前位置: 首页>>代码示例>>PHP>>正文


PHP Collection::groupBy方法代码示例

本文整理汇总了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;
 }
开发者ID:killtw,项目名称:TDD_Day2,代码行数:15,代码来源:ShoppingCart.php

示例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());
     });
 }
开发者ID:yajra,项目名称:laravel-acl,代码行数:18,代码来源:AuthorizesPermissionResources.php

示例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());
 }
开发者ID:sa7bi,项目名称:euro16,代码行数:9,代码来源:SupportCollectionTest.php

示例4: groupByDateFormat

 protected function groupByDateFormat(Collection $backups, string $dateFormat) : Collection
 {
     return $backups->groupBy(function (Backup $backup) use($dateFormat) {
         return $backup->date()->format($dateFormat);
     });
 }
开发者ID:spatie,项目名称:laravel-backup,代码行数:6,代码来源:DefaultStrategy.php


注:本文中的Illuminate\Support\Collection::groupBy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。