本文整理汇总了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;
}
示例2: collection
public function collection(Collection $collection)
{
$this->data = $collection->map(function ($item) {
return $this->makeData($item);
})->all();
return $this;
}
示例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;
}
});
}
示例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;
}
示例5: transformSubscriptions
public function transformSubscriptions(Collection $subscriptions)
{
return $subscriptions->map(function ($subscription) {
return $this->transformSubscription($subscription);
});
}
示例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();
}
示例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;
}
示例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()] ?? []));
});
}
示例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());
}
示例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();
}
示例11: toHumanDiff
public function toHumanDiff()
{
return $this->collection->map(function ($item) {
$item->displayTime = $item->created_at->diffForHumans();
});
}
示例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;
}