fetch additional data in to array from database

爷,独闯天下 提交于 2021-01-05 07:43:04

问题


i have standings array with team info and calculated points. But also i need to get goals count of each team. Need help how to fetch it to current array.

This is my LeaguesController:

public function standings(League $league, Team $team) 
{
    $standings = [
        
    ];

    $matches = Match::where('league_id', '=', $league->id)->get();

    foreach($matches as $match) {
        $homeTeamScore = $match->score->home_team_score;
        $awayTeamScore = $match->score->away_team_score;

        if ($homeTeamScore === $awayTeamScore) {
            if (isset($standings[$match->homeTeam->name])) {
                $standings[$match->homeTeam->name] += 1;
            } else {
                $standings[$match->homeTeam->name] = 1;
            }

            if (isset($standings[$match->awayTeam->name])) {
                $standings[$match->awayTeam->name] += 1;
            } else {
                $standings[$match->awayTeam->name] = 1;
            }
        }

        if ($homeTeamScore > $awayTeamScore) {
            if (isset($standings[$match->homeTeam->name])) {
                $standings[$match->homeTeam->name] += 3;
            } else {
                $standings[$match->homeTeam->name] = 3;
            }

            if (!isset($standings[$match->awayTeam->name])) {
                $standings[$match->awayTeam->name] = 0;
            }
        }

        if ($homeTeamScore < $awayTeamScore) {
            if (isset($standings[$match->awayTeam->name])) {
                $standings[$match->awayTeam->name] += 3;
            } else {
                $standings[$match->awayTeam->name] = 3;
            }

            if (!isset($standings[$match->homeTeam->name])) {
                $standings[$match->homeTeam->name] = 0;
            }
        }
    }

    return view('admin.leagues.standings')->with('standings',$standings);
}

and the array i have:

array:2 [▼
  "secondTeam" => 3
  "firstTeam" => 0
]

i want to do something like this:

array:3 [▼
  "firstTeam" => array:6 [▼
    "points" => 10
    "scoredGoals" => 15
    "goalsConceded" => 20
    "wins" => 20
    "loses" => 20
    "draws" => 20
  ]
  "secondTeam" => array:6 [▼
    "points" => 10
    "scoredGoals" => 15
    "goalsConceded" => 20
    "wins" => 20
    "loses" => 20
    "draws" => 20
  ]
  "ThirdTeam" => array:6 [▼
    "points" => 10
    "scoredGoals" => 15
    "goalsConceded" => 20
    "wins" => 20
    "loses" => 20
    "draws" => 20
  ]
]

How to fetch data to array


回答1:


You could try something like this. You have to be adding for all these extra fields you want:

public function standings(League $league, Team $team)
{
    $standings = [];

    $blank = [
        'points' => 0,
        'scoredGoals' => 0,
        'goalsConceded' => 0,
        'wins' => 0,
        'loses' => 0,
        'draws' => 0,
    ];

    $matches = Match::with('score', 'homeTeam', 'awayTeam')
        where('league_id', '=', $league->id)->get();

    foreach ($matches as $match) {
        $homeTeamScore = $match->score->home_team_score;
        $awayTeamScore = $match->score->away_team_score;

        $standings[$match->homeTeam->name] ??= $blank;
        $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']++;
        }
    }

    return view('admin.leagues.standings')->with('standings',$standings);
}


来源:https://stackoverflow.com/questions/65255578/fetch-additional-data-in-to-array-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!