我有一个前端和管理区域。前端使用Laravel的Auth
软件包,对于Admin
,我使用的是管理员guard
,所有视图,控制器和模型都在admin
目录下。
我正在尝试使用相同的Role
和Permission
模型为前端和管理员用户设置角色和权限。为此,我创建了以下表格
问题:我可以使用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()
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');
});
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();
});
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');
}
}
答案 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
要恢复此状态:
admin_roles
模型中删除User
关系,并将其放置在Admin
模型中。admin_users
模型中的Roles
关系,使其与Admin
模型而不是User
模型建立联系,因此应该像这样
return $this->belongsToMany('App\Admin\Admin');