Laravel-无法获得管理员后卫的角色

时间:2020-01-28 04:28:04

标签: php laravel

我有一个前端和管理区域。前端使用Laravel的Auth软件包,对于Admin,我使用的是管理员guard,所有视图,控制器和模型都在admin目录下。

我正在尝试使用相同的RolePermission模型为前端和管理员用户设置角色和权限。为此,我创建了以下表格

表格

  • 管理员
  • permission_role-数据点
  • 权限
  • role_admin-数据点
  • role_user-数据点
  • 角色
  • 用户

问题:我可以使用belongsToMany()为前端用户获得角色,但不能为管理员获得角色。这是代码。

用户模型

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function admin_roles()
    {
        return $this->belongsToMany('App\Admin\Role', 'role_admin');
    }

    public function roles() {
        return $this->belongsToMany('App\Admin\Role');
    }
}

角色模型

namespace App\Admin;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    protected $fillable = ['name', 'display'];

    public function permissions()
    {
        return $this->belongsToMany('App\Admin\Permission');
    }

    public function admin_users()
    {
        return $this->belongsToMany('App\User');
    }

    public function users()
    {
        return $this->belongsToMany('App\User');
    }

}

前端视图

{{auth()->user()->roles}}

// output 
[{"id":1,"name":"admin","display":"Admin","created_at":null,"updated_at":null,"pivot":{"user_id":1,"role_id":1}},{"id":2,"name":"supervisor","display":"Supervisor","created_at":null,"updated_at":null,"pivot":{"user_id":1,"role_id":2}}]

管理员视图

{{auth()->user()->admin_roles}}
//output
null - checked with dd()

编辑

表结构

role_admin

Schema::create('role_admin', function (Blueprint $table) {
    $table->bigInteger('role_id')->unsigned();
    $table->bigInteger('admin_id')->unsigned();
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
    $table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');
});

role_user

Schema::create('role_user', function (Blueprint $table) {
    $table->bigInteger('role_id')->unsigned();
    $table->bigInteger('user_id')->unsigned();
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

角色

Schema::create('roles', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->text('display');
    $table->timestamps();
});

权限

Schema::create('permissions', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('model');
    $table->string('can');
    $table->timestamps();
});

permission_role

Schema::create('permission_role', function (Blueprint $table) {
    $table->bigInteger('permission_id')->unsigned();
    $table->bigInteger('role_id')->unsigned();

    $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});

管理员

Schema::create('admins', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

管理员模型

 <?php

namespace App\Admin;

use App\Notifications\Admin\ResetPasswordNotification;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }
}

工作代码在这里

我将role_admin的关系设置为先前错误放置在Admin模型中的User模型。

<?php

namespace App\Admin;

use App\Notifications\Admin\ResetPasswordNotification;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }

    public function roles()
    {
        return $this->belongsToMany('App\Admin\Role', 'role_admin');
    }
}

2 个答案:

答案 0 :(得分:1)

似乎您的admin_roles关系在用户模型中,但是在迁移中,您已声明admins表的外键。像这样

$table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');

现在,假设您以admin身份登录,那么您的Admin模型也应该具有这种关系。

public function admin_roles()
{
    return $this->belongsToMany('App\Admin\Role', 'role_admin', 'admin_id', 'role_id');
}

答案 1 :(得分:1)

问题是您使用模型User而不是Admin

要恢复此状态:

  1. admin_roles模型中删除User关系,并将其放置在Admin模型中。
  2. 修改admin_users模型中的Roles关系,使其与Admin模型而不是User模型建立联系,因此应该像这样 return $this->belongsToMany('App\Admin\Admin');