2016-06-23 1 views
1

내 코드에 무엇이 잘못 되었나요? 여기에 내가 때문에 비 개체의 속성을 얻으려고 노력의 $에 datav의 라인 오류가 발생했습니다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(); 

코드입니다. 그러나 아래 $ datax 또는 $ datax-> id 인쇄 할 때 결과입니다.

-

는 DATAX에게 여기

24467 

ID

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

여기 내 HomeController.php의 오류 메시지의 $ 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] => 
) 

(166 라인 145) $

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; 
} 

이 코드에 대해 좌절감을 느끼니 도움을 주셨습니다.

+0

정확한 오류 메시지를 게시 할 수 있습니까? '-> get()'메서드를 호출 할 때'non object'라는 에러 메시지가 나올 수 있습니까? – codedge

+0

뭐, 뭔가 잘못 된 것 같습니다. 1/1 HomeController.php의 ErrorException 154 : 비 객체의 속성을 얻으려고 시도 중 –

+0

원래 게시물을 편집하고 거기에 오류 메시지를 입력하십시오. 또한 HomeComtroller.php를 추가하면 무슨 일이 일어나는지 볼 수 있습니다. – codedge

답변

0

내가 찾은 이상한 해결책을 발견했습니다.

위에서 설명한대로 문제는 $ datax-> id를 사용할 수 없지만 인쇄하는 이유입니다. 여기

는 솔루션입니다 : 당신이 $datax->id

누군가가이 작동 될 수 있는지 설명 할 수없는 $datax['id']으로 사용 할 수 있습니까?

영어로 불편을 드려 죄송합니다. 도움이 되었기를 바랍니다. 귀하는 저의 마지막 질문에도 답변 할 수 있도록 도와 드리겠습니다.

0

$datax['id']처럼 데이터에 액세스하면 $datax어레이 인 경우 정상적으로 수행됩니다. 이 인 경우 $datax->id과 같은 데이터 액세스을 사용합니다.

\App\AccountsScoreHistory 모델은 $model->property으로 모델 속성에 액세스 할 수 있도록 Illuminate\Database\Eloquent\Model을 확장하는 것이 좋습니다.