【发布时间】:2016-09-26 10:45:14
【问题描述】:
我完全被这个问题难住了,我尝试了许多测试来确定问题的根源。在我的控制器中,我为 $players 数组分配了一些值。当我删除变量 opprank 时,一切正常。但是,当我包含它时,我会收到 error 消息
未定义属性:stdClass::$opprank
我已尝试转储 $players 的值,其中 opprank 值按预期显示。我尝试将其设置为静态值“10”而不是 DB 值,但收到相同的错误。希望有人能发现我哪里出错了......
SystemController.php
foreach ($teams as $team)
{
if ($players[$key]->team == $team->id)
{
// Replace Team ID with Team Short Code for Display Purposes
$players[$key]->team = $team->code;
$matchups = DB::table('schedule')->where('week', $week->value)->get();
foreach ($matchups as $matchup)
{
if ($team->id == $matchup->home_team)
{
$opp = DB::table('teams')->select('code', 'rank')->where('id', $matchup->away_team)->first();
$players[$key]->opponent = $opp->code;
$players[$key]->opprank = $opp->rank;
$players[$key]->status = 'Home';
}
else if ($team->id == $matchup->away_team)
{
$opp = DB::table('teams')->select('code', 'rank')->where('id', $matchup->home_team)->first();
$players[$key]->opponent = $opp->code;
$players[$key]->opprank = $opp->rank;
$players[$key]->status = 'Away';
}
}
}
}
foreach ($positions as $position)
{
if ($players[$key]->position == $position->id)
{
$players[$key]->position = $position->short_name;
}
}
}
return $players;
}
}
players.blade.php
<table class="table table-striped table-bordered table-hover" id="dt-players-all">
<thead>
<tr>
<th>Position</th>
<th>Player</th>
<th>Team</th>
<th>Opponent</th>
<th>Game</th>
<th>Opp. Rank</th>
<th>FPPG</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
@foreach ($players as $player)
<tr>
<td>{{$player->position}}</td>
<td>{{$player->name}} <font color="red"><b>{{$player->injury_status}}</b></font></td>
<td>{{$player->team}}</td>
<td>{{$player->opponent}}</td>
<td>{{$player->status}}</td>
<td class="center">
@if ($player->opprank <= 10)
<font color="red"><b>{{$player->opprank}}</b></font>
@elseif ($player->opprank > 10 && ($player->opprank <= 20))
<b>{{$player->opprank}}</b>
@else
<b><font color="green">{{$player->opprank}}</font></b>
@endif
</td>
<td class="center">{{$player->fppg}}</td>
<td class="center">${{$player->salary}}</td>
</tr>
@endforeach
</tbody>
</table>
【问题讨论】:
-
哪一行导致错误?
-
请试试这个 $players[$key][opprank] = $opp->rank;和所有。试试这个。
-
@Phil 它在 player.blade.php 中显示了它的第 49 行,但这只是 if/elseif/else 语句上方的空行。
-
@BrettPowell 在控制器中,在返回之前使用 Log::info($players) 并检查日志信息文件中的输出。即,值是否正确。我们可以据此追踪。
标签: php laravel laravel-5 blade laravel-blade