dongxing6802
2018-04-30 14:39
浏览 45
已采纳

Laravel 5.2使用条件登录

So, I have users table which is default by laravel. But I add a new column named 'status'.
So the columns on the users table are id, name, email, password, remember_token, created_at, updated_at, status
The status values are between 0 and 1. 1 for admin, and 0 for regular user.
Also I have auth, but the problem is how can I check if users' status is 0 or 1 when login so that I can redirect it according to their status? -- admin will go to admin panel and user will go to home

User default by laravel with addition 'status'

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

    class User extends Authenticatable
    {
        use Notifiable;
        protected $fillable = [
            'name', 'email', 'password', 'status',
        ];
        protected $hidden = [
            'password', 'remember_token',
        ];
    }

HomeController also default by laravel

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return view('home');
    }
}

LoginController also default by laravel

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

THANKS.

图片转代码服务由CSDN问答提供 功能建议

因此,我有一个laravel默认的用户表。 但是我添加了一个名为“status”的新列。
所以users表上的列是id,name,email,password,remember_token,created_at,updated_at,status
状态值介于0和1之间,1表示admin,0表示常规 用户。
我也有auth,但问题是如何在登录时检查用户的状态是0还是1,以便我可以根据状态重定向? - admin将转到管理面板,用户将返回主页

用户默认为laravel并添加'status'

 使用Illuminate  \ Notifications \ Notifiable; 
use Illuminate \ Foundation \ Auth \ User as Authenticatable; 
 
类用户扩展Authenticatable 
 {
使用Notifiable; 
 protected $ fillable = [
'name','email',  'password','status',
]; 
 protected $ hidden = [
'密码','remember_token',
]; 
} 
   
 
  

HomeController也默认为laravel

 使用Illuminate \ Http \ Request; 
 
class HomeController扩展Controller 
 {
 public function __construct()
 {\  n $ this->中间件('auth'); 
} 
 
公共函数索引()
 {
返回视图('home'); 
} 
} 
    
 
 

LoginController也默认为laravel

 使用App \ Http \ Controllers \ Controller; 
use Illuminate \ Foundation \ Auth \ AuthenticatesUsers; \  n 
class LoginContr  oller扩展Controller 
 {
使用AuthenticatesUsers; 
 protected $ redirectTo ='/ home'; 
 
 public function __construct()
 {
 $ this-> middleware('guest') - > 除了('logout'); 
} 
} 
   
 
 

感谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtby67541 2018-04-30 14:52
    已采纳

    You can override the authenticated() method in App\Http\Controllers\Auth\LoginController to add logic after the user is authenticated:

       protected function authenticated($request,$user)
        {
            if(\Auth::user()->status){
                return redirect('/admin'); 
            }
    
            return redirect('/home');   
        }
    
    点赞 打赏 评论
  • drv13270 2018-04-30 14:43

    If its all default laravel authentication then you should be able to do

    ( Auth::user()->status === 0 ) ? 'do something' : 'do something else';
    
    点赞 打赏 评论
  • dongxuan58311366668 2018-04-30 14:52

    By default, The laravel redirect to "authenticated" function after login. So you can add "authenticated" function in "LoginController". In the "authenticated" function will get the user object.

    protected function authenticated(Request $request, $user)
    {
        if (!$user->status == 1) {
            return redirect('admin');
        }
        return redirect('web');
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题