What is wrong with him? Here is the error:

Undefined property: Illuminate\Database\Eloquent\Collection::$id 

That's what swears:

 public static function _getUserChanceOfGame($user, $game1x1) { $chance = 0; if (!is_null($user)) { $bet = Bet_1x1::where('game_id', $game1x1->id) ->where('user_id', $user->id) // Ругается сюда ->sum('price'); if ($bet == 0) { $chance = 0; } else { $chance = round($bet / $game1x1->price, 3) * 100; } } return $chance; } 

If something else needs to be thrown off, I will throw it off.

    1 answer 1

    The fact that $ user is not null does not mean that it is an object. Make an interruption and see what in $ user

     public static function _getUserChanceOfGame($user, $game1x1) { $chance = 0; dump($user); if (!is_null($user)) { $bet = Bet_1x1::where('game_id', $game1x1->id) ->where('user_id', $user->id) // Ругается сюда ->sum('price'); if ($bet == 0) { $chance = 0; } else { $chance = round($bet / $game1x1->price, 3) * 100; } } return $chance; } 

    and that would work, put check on object at least

     public static function _getUserChanceOfGame($user, $game1x1) { $chance = 0; if (is_object($user) && property_exists($user,'id')) { $bet = Bet_1x1::where('game_id', $game1x1->id) ->where('user_id', $user->id) // Ругается сюда ->sum('price'); if ($bet == 0) { $chance = 0; } else { $chance = round($bet / $game1x1->price, 3) * 100; } } return $chance; } 
    • 2
      Collection {# 271 ▼ #items: []} Writes - Snegan
    • one
      Empty collection. At the entrance, nothing comes - volcan0