【问题标题】:How i can fix error Object of class Illuminate\Database\Eloquent\Collection could not be converted to int我如何修复 Illuminate\Database\Eloquent\Collection 类的错误对象无法转换为 int
【发布时间】:2020-05-18 12:40:47
【问题描述】:

我的页面应该显示来自不同表格的总和。当我尝试用代码求和时:

        $games = Game::where('winner_id', $user->id)->get();
        $games_low = GameLow::where('winner_id', $user->id)->get();
        $wins = $games + $games_low->count();
        $wins = $wins;
        $totalBank = $games->sum('price');

我收到错误Object of class Illuminate\Database\Eloquent\Collection could not be converted to int。我该如何解决这个问题?

【问题讨论】:

  • 什么是$gamesPlayed
  • 这是从数据库中显示的游戏计数,我不在刀片上使用 $winrate$gamesPlayed 变量,我编辑我的问题。

标签: laravel


【解决方案1】:

问题 - 集合

+ 用于对象 ($games) 和数字 ($games_low->count()) 导致错误。

$games     = Game::where('winner_id', $user->id)->get(); // Collection
$games_low = GameLow::where('winner_id', $user->id)->get(); // Collection

$games_low->count() // int

解决方案

count 用于GameGameLow

$games     = Game::where('winner_id', $user->id)->count();
$games_low = GameLow::where('winner_id', $user->id)->count();
$wins      = $games + $games_low;

dd($wins);

更新

如何正确显示变量的总和?在我有一个变量之前,我使用了$totalBank = $games->sum('price'); 我现在需要如何显示总和$games$games_low,我不知道

$games     = Game::where('winner_id', $user->id);
$games_low = GameLow::where('winner_id', $user->id);
$wins      = $games->count() + $games_low->count();
$totalBank = $games->sum('price');

【讨论】:

  • 是的,它有效!最后一个问题:我如何正确显示变量的总和?在我有一个变量之前,我使用了$totalBank = $games->sum('price'); 我现在需要如何显示总和$games$games_low,我不知道
  • 检查我的答案:)
猜你喜欢
  • 2019-05-29
  • 2015-09-23
  • 2019-06-27
  • 2019-01-10
  • 2021-12-04
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
相关资源
最近更新 更多