首先創(chuàng)建user表,里面有:id, name, password,remember_token等字段。
然后再Models添加表模型User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use DB; class User extends Model implements AuthenticatableContract { protected $table = 'user' ; protected $primarykey = 'id' ; public $timestamps = false; protected $fillable = [ 'user_name' , 'password' , 'user_phone' , 'user_email' , 'user_role_id' , 'user_avart' , 'user_sex' , 'user_age' , 'user_birthday' , 'last_login_ip' , 'last_login_time' , 'is_disabled' , 'remember_token' ]; /** * Get the unique identifier for the user. * * @return mixed */ public function getAuthIdentifier(){ return $this ->getKey(); } /** * Get the password for the user. * * @return string */ public function getAuthPassword(){ return $this ->password; } /** * Get the token value for the "remember me" session. * * @return string */ public function getRememberToken() { return $this ->{ $this ->getRememberTokenName()}; } /** * Set the token value for the "remember me" session. * * @param string $value * @return void */ public function setRememberToken( $value ) { $this ->{ $this ->getRememberTokenName()} = $value ; } /** * Get the column name for the "remember me" token. * * @return string */ public function getRememberTokenName() { return 'remember_token' ; } } |
然后需要在配置文件config/auth.php中修改如下配置:
1
2
|
'model' => App\Models\User:: class , //指定模型 'table' => 'user' , //指定用戶表(user是我數(shù)據(jù)中儲存用戶的表) |
接著在登錄方法里使用Auth::login() 方法登錄,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public function store(Request $request ) { if ( empty ( $request ->get( 'chkCode' )) || trim( $request ->get( 'chkCode' )) != trim(Session::get( 'admincaptcha' ))){ $error ->add( 'result' , '驗證碼不正確' ); return back()->withErrors( $error ); } $adminUser = User::where( 'user_name' , $request ->get( 'user_name' ))->where( 'user_role_id' , '>' , 0)->first(); if ( empty ( $adminUser )){ $error ->add( 'result' , '用戶無后臺權(quán)限' ); return back()->withErrors( $error ); } else { if (md5( $request ->get( 'user_pwd' ))=== $adminUser ->password&& $adminUser ->user_role_id){ Auth::login( $adminUser ); Session::put( 'admincaptcha' , "" ); return redirect()->route( 'admin.home' ); } else { $error ->add( 'result' , '用戶名或密碼錯誤' ); return back()->withErrors( $error ); } } } |
然而雖然這個頁面可以獲取到登錄信息,然而其他頁面卻沒有,原來是因為id和密碼我用的是user_id和user_pwd不是id和password,這兩個必須不能變,改了之后可以正常登錄。
以上這篇解決laravel5中auth用戶登錄其他頁面獲取不到登錄信息的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/leedaning/article/details/53102703