【问题标题】:L5 - Error : Trying to get property of non-object (Weird Case)L5 - 错误:试图获取非对象的属性(奇怪的情况)
【发布时间】:2016-06-23 09:30:49
【问题描述】:

你们能告诉我我的代码有什么问题吗? 这是代码

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

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

由于尝试获取非对象的属性,我在 $datav 的行上出现错误。但是,下面是我打印 $datax 或 $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

这是错误信息

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

这是我的 HomeController.php(第 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;
}

对这段代码感到非常沮丧,感谢任何帮助。

【问题讨论】:

  • 能否请您发布确切的错误信息?也许你在调用->get() 方法时收到non object 错误消息?
  • 糟糕,好像出了点问题。 HomeController.php 第 154 行中的 1/1 ErrorException:试图获取非对象的属性
  • 请编辑您的原始帖子并在此处输入错误消息。还要添加你的 HomeComtroller.php,这样我们就可以看到发生了什么。
  • 如果你输入\App\AccountsScoreHistory::whereRaw('account_score_id=24467')->get();会怎样?
  • 您的::whereRaw() 中没有任何 raw 您是否尝试在那里使用简单的where()

标签: php laravel object laravel-5 eloquent


【解决方案1】:

我找到了我所发现的最奇怪的解决方案。

如上所述,问题是为什么$datax->id不能用只能打印?

这是解决方案:您可以将其用作$datax['id'] 而不是$datax->id

有人能解释一下这是如何工作的吗?

对不起我的英语,我希望这对你有帮助,你也可以帮助我回答我的最后一个问题。

【讨论】:

    【解决方案2】:

    好吧,如果$datax 是一个数组,你通常会访问像$datax['id'] 这样的数据。 如果$datax对象,则访问您使用的$datax->id 之类的数据。

    您的\App\AccountsScoreHistory 模型有望扩展Illuminate\Database\Eloquent\Model 以利用$model->property 访问模型属性。

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2014-05-08
      • 2016-06-13
      • 2018-06-02
      • 2017-08-20
      相关资源
      最近更新 更多