2015-09-10 38 views
0

編輯:我現在正在測試它,很奇怪,有時它工作,有時它會給我那個錯誤。試圖獲得非對象的財產Laravel Auth

我有一個應用程序,我已經覆蓋Laravel註銷功能,所以我有這個在我的AuthController.php

public function getLogout() 
{ 

    $userid = Auth::user()->id; 

    date_default_timezone_set('Asia/Taipei'); 

    $today = date('Y-m-d'); 
    $logHour = new LoginHour(); 
    $checkLogin = $logHour->checkLoginHoursOut(intval($userid), $today); 

    if($checkLogin != null) 
    { 
     $loginhours = ''; 
     $timestamp = date('Y-m-d h:i:s'); 
     $timestamp2 = strtotime($timestamp); 

     $userLastLogin = $checkLogin[0]->timestamp; 
     $userLastLogin2 = strtotime($userLastLogin); 

     // Get difference in hours 
     $diffHours = round(($timestamp2 - $userLastLogin2)/3600, 2); 


     LoginHour::where('date', '=', $today)-> 
        where('user_id', '=', $userid)-> 
        update(['loginhours' => $checkLogin[0]->loginhours + $diffHours, 'status' => 0, 'timestamp' => $timestamp]); 
    }      

    Auth::logout(); 

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/auth/login'); 
} 

但由於某些原因,當我想退出我有這樣的錯誤:

Trying to get property of non-object 

指向我此行

$userid = Auth::user()->id; 

可能是什麼PR oblem?我相信我仍然可以訪問Auth因爲我還沒有在該行之前調用Auth::logout();

+0

當您訪問它時,您始終通過身份驗證? – Jeemusu

+0

@Jeemusu我所做的是登錄然後註銷,我不知道當我剛剛登錄成功,然後嘗試註銷時出現錯誤,我怎麼變得不認證。 – jackhammer013

+0

嘗試訪問其他經過身份驗證的路由上的Auth :: user()進行調試。可能值得將中間件應用到註銷路由,以便未經授權的用戶無法訪問它。 – Jeemusu

回答

0

我想,當您訪問logout路由時,如果沒有登錄用戶,問題就存在。

所以,你應該檢查它的頂部。

public function getLogout() 
{ 
    if (Auth::guest()) return; 

    ... 
} 
+0

如果我登錄然後註銷並出現錯誤,如何訪問註銷路由而不登錄用戶? – jackhammer013

相關問題