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; } } } $res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'minutes_played', 'playerrating_value')->get(); $homes = self::filterMatches($res, $home_matches); $res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'minutes_played', 'playerrating_value')->get(); $aways = self::filterMatches($res, $away_matches); $data = [ 'datasets' => [ [ 'label' => $match->home->name, 'data' => $homes, 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fb2c36', 'fill' => [ 'target' => 1, 'above' => '#fb2c3680', 'below' => '#fdc70080', ], ], [ 'label' => $match->away->name, 'data' => $aways, 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fdc700', 'fill' => false, ], ], 'labels' => $labels, ]; return $data; /** * IF WITH PLAYER */ // $matches_played = 0; // $description = ''; // if ($tm_player_id) { // $matches_played = StatistikoPlayer::where('tm_player_id', (int) $tm_player_id) // ->when($player_on_field, function ($query) use ($player_on_field) { // switch ($player_on_field) { // case 'lineup': // return $query->where('lineup', 'T'); // case 'substituted': // return $query->where('lineup', 'P'); // } // }) // ->pluck('match_id')->toArray(); // if (! $matches_played) { // return []; // } // $description = ' (with '.StatistikoDetail::where('tm_player_id', (int) $tm_player_id)->value('name').')'; // } } public static function filterMatches($res, $matches) { $result = []; foreach ($res as $value) { $match_id = $value->match_id; if (isset($return[$match_id])) { $result[$match_id]['sum_min'] += $value->minutes_played; $result[$match_id]['rating'] += $value->playerrating_value * $value->minutes_played; } else { $result[$match_id]['sum_min'] = $value->minutes_played; $result[$match_id]['rating'] = $value->playerrating_value * $value->minutes_played; } } $return = []; foreach ($matches as $matchid) { if (isset($result[$matchid])) { $return[] = $result[$matchid]['rating'] / $result[$matchid]['sum_min']; } else { $return[] = null; } } return $return; } }