【问题标题】:Laravel Undefined property: stdClass::$opprankLaravel 未定义属性:stdClass::$opprank
【发布时间】: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


【解决方案1】:

在获得$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
}

【讨论】:

  • 是的,因为你在设置 opprank 属性之前使用了 if 语句,所以一些玩家没有它。您还可以在使用模板之前检查模板中是否存在 opprank 属性。
  • 在其他部分添加这一行。
  • if... else if... else { set Null }
  • 我刚试过这个,但我仍然遇到同样的问题。正如我在原始帖子中所提到的,我也已经尝试将变量设置为静态值,以防问题是缺少值但似乎并非如此。
  • @BrettPowell 如果您的逻辑工作正常并产生所需的输出,则 isset 或 !empty 应该可以工作。检查更新的答案。
【解决方案2】:
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;
                   }
                }

【讨论】:

  • 刚刚尝试过,但仍然遇到同样的错误。由于错误消息是“未定义的属性”,因此问题似乎在于未读取实际变量而不是值本身。
  • @BrettPowell 在从控制器文件返回之前使用 Log::info($players)。
猜你喜欢
  • 2016-12-29
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多