2014-09-26 40 views
0

不確定這裏發生了什麼。Laravel - 嘗試在兩次登錄時獲取非對象的屬性

如果我控制器

protected $user; 

public function __construct() 
{ 
    $this->user = Auth::user(); 
} 

public function postLogin() 
{ 
    if(Auth::attempt([ 
     'username'=>Input::get('username'), 
     'password'=>Input::get('password') 
    ])) 
    { 
     return $this->user->username; 
    } 
    else 
    { 
     return Redirect::to('login'); 
    } 
} 

的一個有這個作爲我的登錄功能,並將此作爲我的註銷

public function getLogout() 
{ 
    Auth::logout(); 

    return Redirect::to('login'); 
} 

如果我登錄一次,然後它會顯示用戶名,就像我想。 如果我註銷並重新登錄時,我收到了

ErrorException (E_UNKNOWN) 
Trying to get property of non-object 

然後,如果我註銷並重新登錄它的工作一次,直到我再次註銷。 關於這裏發生了什麼的任何想法?

回答

0

我認爲這個問題是無論是在:

$this->user = Auth::user(); 

return $this->user->username; 

在構造函數中,你倒是應該做這樣的事情:

if (Auth::check()) { 
    $this->user = Auth::user(); 
} 

但在你登錄時不該't use:

return $this->user->username; 

return Auth::user()->username; 
+0

這幫助,謝謝! – PumpkinJack 2014-09-26 06:37:05

相關問題