Laravel 未定义 属性:stdClass::$uprank
Laravel Undefined property: stdClass::$opprank
我完全被这个问题难住了,我已经尝试了很多测试来确定问题的根源。在我的控制器中,我为 $players
数组分配了一些值。当我删除变量 opprank
时,一切正常。但是,当我包含它时,我收到 error 消息
Undefined property: 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>
在你得到 $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
}
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;
}
}
我完全被这个问题难住了,我已经尝试了很多测试来确定问题的根源。在我的控制器中,我为 $players
数组分配了一些值。当我删除变量 opprank
时,一切正常。但是,当我包含它时,我收到 error 消息
Undefined property: 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>
在你得到 $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
}
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;
}
}