drtoclr046994545 2016-06-23 09:30
浏览 65
已采纳

L5 - 错误:试图获取非对象的属性(奇怪的情况)

can you guys show me what's wrong with my code? here's the code

$datax = \App\AccountsScore::where('account_id', $account_id)
    ->where('score_id', $score->id)
    ->first();

$datav = \App\AccountsScoreHistory::where(
    'account_score_id', $datax->id
)->get();

i got error on $datav 's line because of Trying to get property of non-object. however, below is the result when i print $datax or $datax->id.

$datax

App\AccountsScore Object (
[connection:protected] => riskserver
[table:protected] => accounts_score
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array (
    [id] => 24467
    [account_id] => 114
    [score_id] => 14
    [value] => 8 )
[original:protected] => Array (
    [id] => 24467
    [account_id] => 114
    [score_id] => 14
    [value] => 8 )
[relations:protected] => Array ( )
[hidden:protected] => Array ( )
[visible:protected] => Array ( )
[appends:protected] => Array ( )
[fillable:protected] => Array ( )
[guarded:protected] => Array (
    [0] => * )
[dates:protected] => Array ( )
[dateFormat:protected] =>
[casts:protected] => Array ( )
[touches:protected] => Array ( )
[observables:protected] => Array ( )
[with:protected] => Array ( )
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
) 

$datax->id

24467

here's the error messages

Whoops, looks like something went wrong. 
1/1 ErrorException in HomeController.php line 154: Trying to get property of non-object

here's my HomeController.php (Line 145 - 166)

function getTotalScore($name='', $account_id)
{
    if(strlen($name) > 0)
    {
        $score = Score::where('name', $name)->first();
        if(!$score) return 0 ;

        $score_id = $score->id;
        $datax = \App\AccountsScore::where('account_id', $account_id)->where('score_id', $score->id)->first();
        /* line 154 */ $datav = \App\AccountsScoreHistory::whereRaw('account_score_id = '.$datax->id)->get();
        $countdatas = count($datav);
        if($countdatas == null){
            $countdatas = 0;
        }
        return $countdatas;
    }
    else{
        $data = AccountsScore::select(DB::raw('ifnull(sum(value),0) AS total'))->where('account_id', $account_id)->first();
        return $data->total;
    }
    return 0;
}

So frustating about this code, any help appreciated.

  • 写回答

2条回答 默认 最新

  • dtcrw26206 2016-06-24 04:02
    关注

    I found the weirdest solution i've ever found.

    as explained above, the problem is why $datax->id can't be used but print?

    here is the solution : you can using it as $datax['id'] not $datax->id

    can someone explain how this could be works?

    sorry for my english, i hope this would be helpful for you and you can help me to answer my last question too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名