0

i have an array, where all data is calculated by records from matches table:

Illuminate\Support\Collection {#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,
]);
3
  • How do you know which element in the array belongs to which team? Is the element key (First team) the name of the team in the teams table? Commented Jan 29, 2021 at 15:00
  • @Unflux, yes, array element key "First team" is value from teams table, where value "name" Commented Jan 29, 2021 at 15:03
  • well doesn't that code look familiar Commented Jan 29, 2021 at 15:46

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

i added my code below, maybe you can help with editing them. I would be grateful
@Jetz Best to add it to your original question rather than as an answer buddy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.