In this function there is a value and it is not null. But when we put the same value in the variable and print the variable, it says that the variable is null.
public function h2h($game_id)
{
$game = Game::find($game_id);
$team1 = $game->team1;
$team2 = $game->team2;
$games = Game::where('team1', $team1)
->where('team2', $team2)
->orWhere('team1', $team2)
->where('team2', $team1)->get();
$h2h = [];
foreach ($games as $game) {
$team1Goals = count(json_decode($game->stat->team1Goals));
$team2Goals = count(json_decode($game->stat->team2Goals));
$h2h[] = [
'date' => verta($game->date)->format('l d M'),
'team1' => [
'name' => $game->home->name,
'logo' => $game->home->logo,
'goals' => $team1Goals
],
'team2' => [
'name' => $game->away->name,
'logo' => $game->away->logo,
'goals' => $team2Goals
],
];
}
dd($h2h);
return $h2h;
}
The problem is:
count(json_decode($game->stat->team1Goals));
This piece of code, when assigned to a variable, says it is null, but when you print it, the correct value is printed.