【发布时间】: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