我有自定义登录功能在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