douqie1816 2016-09-26 10:45
浏览 169
已采纳

Laravel Undefined属性:stdClass :: $ opprank

I am absolutely stumped by this issue, where I have tried numerous tests to determine the source of the problem. In my Controller, I assign a number of values to my $players array. When I remove the variable opprank everything works fine. When I have it included however, I get the error message

Undefined property: stdClass::$opprank

I have tried dumping the values of $players where the opprank value is shown as expected. I have tried setting it to a static value of '10' as opposed to the DB value but receive the same error. Hopefully someone can spot where I am going wrong...

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>
  • 写回答

2条回答 默认 最新

  • dongyuhui0418 2016-09-26 10:51
    关注

    Initialise it to a NULL value or to some value you need just after you get $key - I think it should be inside some foreach() loop not visible in the question.

    $players[$key]->opprank = NULL;
    

    As I think you are getting this error because you set the value of opprank in some condition, which fails to assign if the condition is not executed. If it is just a warning, assign it in this way.

    Also try adding in the else part (if this works - will be the best fix)

    if () {
      // your code
    }
    else if () {
      // your code
    }
    else {
      $players[$key]->opprank = NULL;
    }
    

    At the last try you can obviously do an isset() or !empty() check for them. Only if your output is fine and you just want to get rid of warnings.

    if(isset($player->opprank)) {
      // existing code for if, else if and else
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)