Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 根据身份验证用户的角色重定向他们_Laravel - Fatal编程技术网

Laravel 根据身份验证用户的角色重定向他们

Laravel 根据身份验证用户的角色重定向他们,laravel,Laravel,我需要根据应用程序中的角色重定向用户。在MySQL中,我在Users表中添加了“isAdmin”列来检查用户是否为admin。我提出的唯一解决方案是根据用户类型在同一刀片上呈现html。 比如说 @if(Auth::user()->isAdmin == 1) <p>Hello, Admin</p> //other html for admin @else //other html for users @if(Auth::user()->isAdmin==1) 你

我需要根据应用程序中的角色重定向用户。在MySQL中,我在Users表中添加了“isAdmin”列来检查用户是否为admin。我提出的唯一解决方案是根据用户类型在同一刀片上呈现html。 比如说

@if(Auth::user()->isAdmin == 1)
<p>Hello, Admin</p>
//other html for admin

@else
//other html for users
@if(Auth::user()->isAdmin==1)
你好,管理员

//用于管理的其他html @否则 //用户的其他html
有没有更好的方法可以做到这一点,因为它看起来很凌乱,所以代码更漂亮、更高效? 我正在使用这个包(),它使用Laravel的默认身份验证系统


我想在登录过程中将用户重定向到两个不同的刀片服务器,比如homeUsers.blade.php和admin.blade.php。

逻辑如下:最好的方法是创建两个表,一个是
用户
,另一个是
角色
,并将
role\u id
作为外键放入users表中。然后创建一个角色
属于用户的关系

但是,在您的情况下,如果您有两个视图,并且希望为管理员显示一个视图,为其他用户显示另一个视图,请遵循以下方法

创建两个要在案例中使用的视图1)管理员2)家庭用户

@if(Auth::user()->isAdmin == 1)
  @include('admin') //admin.blade.php
@else
 @include('homeUsers') //homeUsers.blade.php

我不确定您使用的是什么版本的Laravel,但这似乎应该在您的中间件中

检查中间件中的
RedirectIfAuthenticated.php
。然后,您可以执行以下操作

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        if (Auth::user()->role == 'Admin')
        {
            return redirect('/admin');
        }
        else
        {
            return redirect('/home')
        }
    }

    return $next($request);
}

这似乎是我最好的选择。我尝试了@Daniel提出的解决方案,但在我的情况下不起作用。也许我做错了什么,但现在我将使用你的解决方案。我忘了提及其他可能有同样问题的人。它应该是
@include('admin')
,而不是
@include('admin.blade.php')
。(无延期)