我有自定義登錄功能在Laravel 5.4中似乎工作,但不完全。我有UserController.php是會話登錄的用戶返回undefined
public function loginSubmit()
{
$user = User::where('username', Input::get('username'))->first();
if (!$user) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
if (!Hash::check(Input::get('password'), $user->password)) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
$user->last_login = \Carbon\Carbon::now();
$user->save();
Session::put('user', ['user_id' => $user->user_id]);
//dd(Session::get('user', null));
return Redirect::to('/');
}
dd(Session::get('user', null));
返回
array:1 [▼
"user_id" => 1
]
這意味着,與ID = 1的用戶登錄,並在存儲在會話。在BaseController.php
這分享我有這個
public static function isLoggedIn()
{
$user = Session::get('user', null);
if ($user !== null) {
return true;
} else {
return false;
}
}
用戶會話但是,當我試圖表明的用戶名登錄的用戶
{{ $user->username }}
我有錯誤
未定義的變量:用戶
這是我的用戶模型
namespace App;
use Illuminate\Database\Eloquent\Model;
use Eloquent;
use DB;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
protected $primaryKey = 'user_id';
}
任何想法我的會議在這裏有什麼問題嗎?
是否需要爲用戶指定這樣的會話?你可以用'Auth :: user()'來訪問登錄的用戶,或者檢查他們是否登錄了'Auth :: check()'。 – morph
'{{Auth :: user() - > username}}'正在返回'試圖獲取非對象的屬性' – Peter
用'Auth :: login($ user)替換'Session :: put()'' – morph