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


PHP Collection::map方法代码示例

本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection::map方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::map方法的具体用法?PHP Collection::map怎么用?PHP Collection::map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Illuminate\Database\Eloquent\Collection的用法示例。


在下文中一共展示了Collection::map方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: transformCollection

 /**
  * @param $items
  * @return array
  */
 public function transformCollection(Collection $items, $showHref = false)
 {
     $data = $items->map(function ($item) use($showHref) {
         return $this->transform($item, $showHref);
     })->toArray();
     return $data;
 }
开发者ID:kchunterdeluxe,项目名称:AutioBackend,代码行数:11,代码来源:Transformer.php

示例2: collection

 public function collection(Collection $collection)
 {
     $this->data = $collection->map(function ($item) {
         return $this->makeData($item);
     })->all();
     return $this;
 }
开发者ID:jackweinbender,项目名称:laravel-jsonapi,代码行数:7,代码来源:JsonApi.php

示例3: listRelation

 protected function listRelation(Collection $collection, array $fields)
 {
     return $collection->map(function ($item) use($fields) {
         if (count($fields) == 1) {
             return $item->{$fields[0]};
         } else {
             $items = [];
             foreach ($fields as $field) {
                 array_push($items, $item->{$field});
             }
             return $items;
         }
     });
 }
开发者ID:wasgeht-berlin,项目名称:website,代码行数:14,代码来源:BaseTransformer.php

示例4: menusOrmToJsTreeJson

 /**
  * @param array $menusCol
  * @return string
  */
 private function menusOrmToJsTreeJson(Collection $menusCol)
 {
     $jsTreeCol = $menusCol->map(function ($item, $key) {
         $id = $item->id;
         $parent_id = $item->parent_id;
         $label = $item->label;
         $icon = $item->icon;
         // Fix attribute of root item for JSTree
         if ($id == $parent_id && 'Root' == $label) {
             $parent_id = '#';
         }
         return collect(['id' => $id, 'parent' => $parent_id, 'text' => $label, 'icon' => $icon]);
     });
     $menusJson = $jsTreeCol->toJson();
     return $menusJson;
 }
开发者ID:sroutier,项目名称:menu-builder,代码行数:20,代码来源:MenusController.php

示例5: transformSubscriptions

 public function transformSubscriptions(Collection $subscriptions)
 {
     return $subscriptions->map(function ($subscription) {
         return $this->transformSubscription($subscription);
     });
 }
开发者ID:SenhorBardell,项目名称:yol,代码行数:6,代码来源:transformer.php

示例6: serializePermissions

 /**
  * Serialize a collection of permission models into a plain array.
  *
  * @param  \Illuminate\Database\Eloquent\Collection  $permissions
  * @return array
  */
 protected function serializePermissions(Collection $permissions)
 {
     return $permissions->map(function ($permission) {
         return $permission->getAttributes();
     })->all();
 }
开发者ID:devonzara,项目名称:bouncer,代码行数:12,代码来源:CachedClipboard.php

示例7: tree

 /**
  * Buiding collections to tree.
  *
  * @param  Collection $source
  * @return object
  */
 public function tree(Collection $source)
 {
     $source->load(implode('.', array_fill(0, 20, 'children')));
     $source->map(function ($item) {
         $item->children->sortBy('sort');
     });
     return $source;
 }
开发者ID:sharenjoy,项目名称:cmsharenjoy,代码行数:14,代码来源:Categorize.php

示例8: map

 protected function map(EloquentCollection $result, Collection $group)
 {
     $group = $group->reduce(function (Collection $result, $item) {
         $result[data_get($item, $this->relationKey)] = $item;
     }, new Collection());
     return $result->map(function (Model $item) use($group) {
         $item->setRelation($this->relation, $this->getRelated()->newCollection($group[$item->getKey()] ?? []));
     });
 }
开发者ID:znck,项目名称:plug,代码行数:9,代码来源:MorphToAny.php

示例9: makeExecuteRequest

 /**
  * Make a new "execute" request instanse with nested requests.
  *
  * @param Collection $requests
  * @return ExecuteRequest
  */
 protected function makeExecuteRequest(Collection $requests)
 {
     $clientRequests = $requests->map(function (VkRequest $request) {
         return new Request($request->method, $request->parameters);
     });
     return ExecuteRequest::make($clientRequests->all());
 }
开发者ID:atehnix,项目名称:laravel-vk-requester,代码行数:13,代码来源:SendBatch.php

示例10: serializeAbilities

 /**
  * Serialize a collection of ability models into a plain array.
  *
  * @param  \Illuminate\Database\Eloquent\Collection  $abilities
  * @return array
  */
 protected function serializeAbilities(Collection $abilities)
 {
     return $abilities->map(function ($ability) {
         return $ability->getAttributes();
     })->all();
 }
开发者ID:JosephSilber,项目名称:bouncer,代码行数:12,代码来源:CachedClipboard.php

示例11: toHumanDiff

 public function toHumanDiff()
 {
     return $this->collection->map(function ($item) {
         $item->displayTime = $item->created_at->diffForHumans();
     });
 }
开发者ID:codeforpg,项目名称:PostIt,代码行数:6,代码来源:DateFormat.php

示例12: collection

 /**
  * @return Collection
  */
 public function collection(EloquentCollection $collection)
 {
     $standings = new Collection();
     $collection->map(function ($match) use($standings) {
         $homeTeam = $standings->pull($match->homeTournamentTeam->id);
         $awayTeam = $standings->pull($match->awayTournamentTeam->id);
         $defaultTeamData = ['matches' => 0, 'position' => 0, 'wins' => 0, 'draws' => 0, 'losts' => 0, 'points' => 0, 'goalsScored' => 0, 'goalsAgainsted' => 0, 'goalsDifference' => 0];
         if (!$homeTeam) {
             $homeTeam = array_merge(['teamId' => $match->homeTournamentTeam->id, 'name' => $match->homeTournamentTeam->team->name], $defaultTeamData);
         }
         if (!$awayTeam) {
             $awayTeam = array_merge(['teamId' => $match->awayTournamentTeam->id, 'name' => $match->awayTournamentTeam->team->name], $defaultTeamData);
         }
         if (Match::STATUS_FINISHED == $match->status) {
             $homeTeam['matches']++;
             $awayTeam['matches']++;
             $homeTeam['goalsScored'] += $match->homeScore;
             $homeTeam['goalsAgainsted'] += $match->awayScore;
             $homeTeam['goalsDifference'] = $homeTeam['goalsScored'] - $homeTeam['goalsAgainsted'];
             $awayTeam['goalsScored'] += $match->awayScore;
             $awayTeam['goalsAgainsted'] += $match->homeScore;
             $awayTeam['goalsDifference'] = $awayTeam['goalsScored'] - $awayTeam['goalsAgainsted'];
             switch ($match->resultType) {
                 case Match::RESULT_TYPE_HOME_WIN:
                     $homeTeam['wins']++;
                     $homeTeam['points'] += Match::POINTS_WIN;
                     $awayTeam['losts']++;
                     break;
                 case Match::RESULT_TYPE_AWAY_WIN:
                     $awayTeam['wins']++;
                     $homeTeam['losts']++;
                     $awayTeam['points'] += Match::POINTS_WIN;
                     break;
                 case Match::RESULT_TYPE_DRAW:
                     $homeTeam['draws']++;
                     $awayTeam['draws']++;
                     $homeTeam['points'] += Match::POINTS_DRAW;
                     $awayTeam['points'] += Match::POINTS_DRAW;
                     break;
             }
         }
         $standings->put($match->homeTournamentTeam->id, $homeTeam);
         $standings->put($match->awayTournamentTeam->id, $awayTeam);
     });
     // sort by points and goal difference
     $standings = $standings->sort(function ($a, $b) {
         if ($b['points'] === $a['points']) {
             return $b['goalsDifference'] - $a['goalsDifference'];
         }
         return $b['points'] - $a['points'];
     });
     $previousRow = null;
     $position = 1;
     $standings = $standings->map(function ($row) use(&$previousRow, &$position) {
         if ($previousRow && $previousRow['points'] > 0 && $previousRow['points'] == $row['points'] && $previousRow['goalsDifference'] == $row['goalsDifference'] && $previousRow['goalsScored'] == $row['goalsScored']) {
             $row['position'] = $previousRow['position'];
         } else {
             $row['position'] = $position;
         }
         $position++;
         $previousRow = $row;
         return $row;
     });
     // alphabetical sort for teams on the same position
     $standings = $standings->sortBy(function ($team) {
         return $team['position'] . '-' . $team['name'];
     }, SORT_NUMERIC);
     return $standings;
 }
开发者ID:bashmach,项目名称:ggf,代码行数:72,代码来源:StandingsSerializer.php


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