나는 먼 관계를 잡으려고 노력하고 그것은 작동하지 않습니다. hasManyThrough
일을 시도했지만 belongsToMany
관계에서 작동하지 않는 것으로 보입니다. 쿼리 오류가 발생했습니다. 여기 멀리 떨어진 belongsToMany 통해 belongsToMany
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id');
$table->integer('user_id');
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('group');
$table->string('name');
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id');
$table->integer('role_id');
});
내 모델입니다 : 내가 할 노력하고있어
class User extends AuthUser
{
//
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
class Role extends Model
{
//
public function permissions()
{
return $this->belongsToMany('App\Permission');
}
}
이 나에게 Property [permissions] does not exist on this collection instance.
오류를 제공
$user = App\User::find(1);
Log::info($user->roles->permissions);
여기
내 마이그레이션합니다.역할을 통해 사용자 권한을 얻으려면 어떻게해야합니까?
편집 :
if (auth()->check()) {
if (auth()->user()->roles->contains('name', 'Admin')) {
Gate::before(function() {
return true;
});
}
else {
$permissions = [];
foreach (auth()->user()->roles as $role) {
$permissions = array_merge($permissions, $role->permissions->pluck('name')->all());
}
foreach (array_unique($permissions) as $permission) {
Gate::define($permission, function() {
return true;
});
}
}
}
가 어떻게이 문제를 개선 할 수 있습니다
내가 할 노력하고있어이 추한 솔루션을 함께 왔어요?
사용자 및 역할은 다 대다 관계가 있습니까? –
당신의 관계는 어떻습니까? 한 사용자가 많은 역할을 갖고 그 역할에 많은 권한이 있습니까? –
@ ImmortalDude 예. – kjdion84