2016-04-27 76 views
4

我在laravel中調用用戶時遇到問題,$user假設是包含所有數組元素的數組,而自動登錄以下代碼的結果爲false。保存在數據庫中的密碼是hash::make('password')如何在laravel中註冊後自動登錄

$user_id = $this->user_model->addUser($user); 
$post = array('password' => $pass_for_auth, 'email' => $email); 
var_dump(Auth::attempt($post,true));die; 

任何一個可以告訴我什麼是正確的問題

回答

4

您可以嘗試通過他$user_id登錄用戶。所以你的代碼是:

$user_id = $this->user_model->addUser($user); 
$post = array('password' => $pass_for_auth, 'email' => $email); 
Auth::loginUsingId($user_id); 

你創建了用戶,所以它返回一個user_id,用user_id你可以登錄用戶。

希望這個作品!

的更多信息:https://laravel.com/docs/5.2/authentication#other-authentication-methods

4

這是做用戶註冊的標準方法 - 創建一個新的用戶模型實例,並把它傳遞給AUTH ::登錄():

// do not forget validation! 
$this->validate($request, [ 
      'name' => 'required|max:255', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|confirmed|min:6', 
     ]); 

$data = $request->all(); 

$user = User::create([ 
      'name' => $data['name'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
     ]); 

Auth::login($user); 
+0

什麼這個認證等級?你能指定完整的命名空間嗎? –

+0

@RameshPareek不需要它。它有一個別名。只是'使用認證;'它。如果你真的想要,你可以在'config/app.php'的'aliases'鍵中找到路徑,它是:'Illuminate \ Support \ Facades \ Auth'。 – totymedli