我对这个问题感到非常难过,我已经尝试了很多测试来确定问题的根源。在我的Controller中,我为$players
数组分配了许多值。当我删除变量opprank
时,一切正常。但是当我收到它时,我收到错误消息
未定义属性: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>
答案 0 :(得分:3)
在获得$key
之后将其初始化为NULL值或某个值 - 我认为它应该位于问题中看不到的某个foreach()循环中。
$players[$key]->opprank = NULL;
我认为您收到此错误是因为您在某些条件下设置了opprank
的值,如果条件未执行则无法分配。如果它只是一个警告,请以这种方式分配。
另外尝试添加else部分(如果这样做 - 将是最好的修复)
if () {
// your code
}
else if () {
// your code
}
else {
$players[$key]->opprank = NULL;
}
在最后一次尝试中,您显然可以对它们执行isset()或!empty()检查。只有你的输出很好,你只想摆脱警告。
if(isset($player->opprank)) {
// existing code for if, else if and else
}
答案 1 :(得分:1)
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';
}
else{
$players[$key]->opprank = NULL;
}
}