2016-05-23 18 views
0

我有一個名爲active_user的列以顯示用戶何時在線(1)。當我登錄用戶時,我將active_user列設置爲1,但是當我登錄用戶並檢查active_user時,它仍然顯示在線(1)。我想要的是,當我單擊註銷按鈕時,aactive_user列將更改爲脫機(0)並將其更新到數據庫。我的登錄信息如下。如何在退出時更改laravel中的列的值

public function postSignIn(Request $request) { 

    $this->validate($request, [ 

     'email' => 'required', 
     'password' => 'required' 

    ]); 

    if(Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) { 
     $user=User::whereRaw('email=?',[Input::get('email')])->first(); 

     //this is where i set the active_user to 1 
     $user->active_user=1; 
     $user->save(); 

     if($user!=null){ 
      switch($user->role_id){ 
       case 1: 
        if($user->status==0){ 
         session()->flash('flash_message_error', ' Your Account has been deactivated. Contact the administrator'); 
         return Redirect::to('admin/login'); 
        } 
        return redirect()->route("index"); 
       break; 

然後下面是註銷功能我註銷功能

public function LogOut($id) { 
    $user = User::find($id); 
    $user->active_user=1; 
    $user->save(); 
    Auth::logout(); 

    return redirect('admin/login'); 
} 

我想改變active_user回到離線(0),但我不斷收到錯誤「缺少參數1爲App \的Http \ Controllers \ AdminController :: LogOut()「

+0

您可以使用登錄/註銷事件..看看這個公認的答案:http://stackoverflow.com/questions/36767556/laravel-auth-count-user-login/36767937#36767937 –

+0

什麼我試圖做的是更新active_user返回到離線(0)當我clcik註銷按鈕 –

回答

0

您的路由註銷現在預計傳遞給它的id屬性。這對我來說似乎是錯誤的,因爲它是您想要應用此邏輯的已認證用戶,必須將他們的ID傳遞給註銷是不必要的。例如,如果你登錄的用戶標識1,與您的代碼註銷他們需要去一個網址,如:

http://myapp.app/logout/1 

相反,使用驗證執行修改的用戶。

// Remove requirement for ID as a parameter 
public function LogOut() { 
    $user = Auth::user(); 
    $user->active_user=1; 
    $user->save(); 
    Auth::logout(); 

    return redirect('admin/login'); 
} 
+0

非常感謝@DavidBarker我認爲這是我的問題,因爲我的路線期待'id'。它現在正在工作:)。我很感激 –

相關問題