你們能告訴我我的代碼有什麼問題嗎? 這裏的代碼L5 - 錯誤:試圖獲取非對象的屬性(奇怪的情況)
$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'錯誤信息? – codedge
哎呀,看起來像是出了點問題。 1/1 HomeController.php中的ErrorException異常第154行:試圖獲取非對象的屬性 –
請編輯您的原始文章並在其中輸入錯誤消息。還要添加你的HomeComtroller.php,這樣我們就可以看到發生了什麼。 – codedge