how to add additional value to array
i have an array, where all data is calculated by records from matches table:
IlluminateSupportCollection {#1342 ▼ #items: array:4 [▼ "First team" => & array:6 [▼ "points" => 3 "scoredGoals" => 6 "goalsConceded" => 6 "wins" => 0 "loses" => 0 "draws" => 3 ] "Second team" => array:6 [▶] "third team" => array:6 [▶] "fourth team" => & array:6 [▶] ] }
i need add to array image of each team (from teams table, where column image) how can i do that?
here is my code from controller, where all data is calculated from matches table:
there is my code which i need edit:
$standings = [];
$blank = [ 'points' => 0, 'scoredGoals' => 0, 'goalsConceded' => 0, 'wins' => 0, 'loses' => 0, 'draws' => 0, ]; $matches = Match::with('score', 'homeTeam', 'awayTeam') ->whereHas('score', function($query){ $query->whereNotNull('home_team_score') ->whereNotNull('away_team_score'); }) ->where('league_id', '=', $league->id) ->get(); foreach ($matches as $match) { $homeTeamScore = $match->score->home_team_score; $awayTeamScore = $match->score->away_team_score; if (! isset($standings[$match->homeTeam->name])) { $standings[$match->homeTeam->name] = $blank; } if (! isset($standings[$match->awayTeam->name])) { $standings[$match->awayTeam->name] = $blank; } $home = &$standings[$match->homeTeam->name]; $away = &$standings[$match->awayTeam->name]; $away['scoredGoals'] += $awayTeamScore; $home['scoredGoals'] += $homeTeamScore; $away['goalsConceded'] += $homeTeamScore; $home['goalsConceded'] += $awayTeamScore; switch ($homeTeamScore <=> $awayTeamScore) { case -1: // home lost // swap home and away and let it fall through $tmpHome = &$home; $home = &$away; $away = &$tmpHome; case 1: // home won $home['points'] += 3; $home['wins']++; $away['loses']++; break; default: // draw $home['points']++; $away['points']++; $home['draws']++; $away['draws']++; } } $standings = collect($standings)->sort(function ($one, $other) { if ($one['points'] !== $other['points']) { return $other['points'] - $one['points']; // similar to desc } $oneDelta = $one['scoredGoals'] - $one['goalsConceded']; $otherDelta = $other['scoredGoals'] - $other['goalsConceded']; return $otherDelta - $oneDelta; // similar to desc }); return view('admin.leagues.standings')->with([ 'standings' => $standings, ]);
Answer
Going with the key
of each element in your collection
is the name of your team
and is stored in the name
column of your teams
table, you can map over your collection and add in your image
.
For example:
$images = [ 'First team' => 'first-team.jpg', 'Second team' => 'second-team.jpg', 'Third team' => 'third-team.jpg' ]; $teamsWithImages = collect([ "First team" => [ "points" => 3, "scoredGoals" => 6, "goalsConceded" => 6, "wins" => 0, "loses" => 0, "draws" => 3, ], "Second team" => [ "points" => 3, "scoredGoals" => 6, "goalsConceded" => 6, "wins" => 0, "loses" => 0, "draws" => 3, ], "Third team" => [ "points" => 3, "scoredGoals" => 6, "goalsConceded" => 6, "wins" => 0, "loses" => 0, "draws" => 3, ] ])->map(function ($item, $key) use ($images) { // You would uncomment this line to retrieve the image // from your teams table // You also wouldn't need the use ($images) either //$item['image'] = Teams::where('name', $key)->first()->image; $item['image'] = $images[$key]; return $item; })->all(); dump($teamsWithImages);
Update
Based on the code you’ve added, you won’t need to map
you can just add the image in your foreach
:
if (! isset($standings[$match->homeTeam->name])) { $standings[$match->homeTeam->name] = $blank; $standing[$match->homeTeam->name]['image'] = $match->homeTeam->image; } if (! isset($standings[$match->awayTeam->name])) { $standings[$match->awayTeam->name] = $blank; $standing[$match->awayTeam->name]['image'] = $match->awayTeam->image; }
Alternatively you could still use map
once you have the standings
sorted, but you may as well just add the image in with everything else.
$standingsWithImages = $standings ->map(function ($item, $key) { $item['image'] = Team::where('name', $key)->first()->image; return $item; })->all();