with('away')->where('id', $match_id)->first(); $home_id = $match->home_id; $away_id = $match->away_id; $res = StatistikoEvent::where('league_id', $league_id)->get(); $home_matches = []; $away_matches = []; $labels = []; foreach ($res as $val) { if ($val->home_id == $home_id or $val->away_id == $home_id or $val->home_id == $away_id or $val->away_id == $away_id) { $labels[] = round($val->starttime->diffInDays(Carbon::now())); $label_formatted[$val->match_id] = $val->starttime->format('d. F Y H:i'); if ($val->home_id == $home_id or $val->away_id == $home_id) { $home_matches[] = $val->match_id; } else { $home_matches[] = null; } if ($val->home_id == $away_id or $val->away_id == $away_id) { $away_matches[] = $val->match_id; } else { $away_matches[] = null; } } } // dd($away_matches); $res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get(); $homes = self::filterMatches($res, $home_matches); $res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get(); $aways = self::filterMatches($res, $away_matches); $data = [ 'datasets' => [ [ 'label' => $match->home->name, 'data' => $homes['all'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fb2c36', 'backgroundColor' => '#fb2c36', ], [ 'label' => 'Defence', 'data' => $homes['def'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, ], [ 'label' => 'Attak', 'data' => $homes['att'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, ], [ 'label' => 'Midfield', 'data' => $homes['mid'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, ], [ 'label' => $match->away->name, 'data' => $aways['all'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fdc700', 'backgroundColor' => '#fdc700', 'fill' => false, ], [ 'label' => 'Defence', 'data' => $aways['def'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, ], [ 'label' => 'Attak', 'data' => $aways['att'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, ], [ 'label' => 'Midfield', 'data' => $aways['mid'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, ], ], 'labels' => $labels, ]; return $data; } public static function filterMatches($res, $matches) { $result = []; foreach ($res as $value) { $match_id = $value->match_id; if (! isset($result[$match_id])) { $result[$match_id]['all']['sum_min'] = 0; $result[$match_id]['all']['rating'] = 0; $result[$match_id]['att']['sum_min'] = 0; $result[$match_id]['att']['rating'] = 0; $result[$match_id]['mid']['sum_min'] = 0; $result[$match_id]['mid']['rating'] = 0; $result[$match_id]['def']['sum_min'] = 0; $result[$match_id]['def']['rating'] = 0; } $result[$match_id]['all']['sum_min'] += $value->minutes_played; $result[$match_id]['all']['rating'] += $value->playerrating_value * $value->minutes_played; if ($value->player_position == 'M') { $result[$match_id]['mid']['sum_min'] += $value->minutes_played; $result[$match_id]['mid']['rating'] += $value->playerrating_value * $value->minutes_played; } elseif ($value->player_position == 'F') { $result[$match_id]['att']['sum_min'] += $value->minutes_played; $result[$match_id]['att']['rating'] += $value->playerrating_value * $value->minutes_played; } elseif ($value->player_position == 'D') { $result[$match_id]['def']['sum_min'] += $value->minutes_played; $result[$match_id]['def']['rating'] += $value->playerrating_value * $value->minutes_played; } } $return = []; foreach ($matches as $matchid) { if (isset($result[$matchid])) { $return['all'][] = $result[$matchid]['all']['rating'] / $result[$matchid]['all']['sum_min']; if ($result[$matchid]['def']['sum_min']) { $return['def'][] = $result[$matchid]['def']['rating'] / $result[$matchid]['def']['sum_min']; } else { $return['def'][] = null; } if ($result[$matchid]['att']['sum_min']) { $return['att'][] = $result[$matchid]['att']['rating'] / $result[$matchid]['att']['sum_min']; } else { $return['att'][] = null; } if ($result[$matchid]['mid']['sum_min']) { $return['mid'][] = $result[$matchid]['mid']['rating'] / $result[$matchid]['mid']['sum_min']; } else { $return['mid'][] = null; } } else { $return['all'][] = null; $return['def'][] = null; $return['att'][] = null; $return['mid'][] = null; } } return $return; } }