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

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.

  • 写回答

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');   
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)