简体   繁体   English

laravel 中的权限方法和 spatie 和用户表相互冲突

[英]permissions method in laravel and spatie and user table conflicting with each other

We are using laravel spatie permissions as the library to manage the role and permissions.我们使用 laravel spatie 权限作为管理角色和权限的库。 Unfortunately before implementation of this library we were having a column in user table as permissions where we were managing the permissions.不幸的是,在实现这个库之前,我们在用户表中有一列作为我们管理权限的权限。 But now when we implemented this library it is getting conflicted with the library.但是现在当我们实现这个库时,它与库发生了冲突。 We tried renaming the existing column that works fine for the library.我们尝试重命名适用于库的现有列。 But implementing this at complete project is impossible as there is full flow working on existing column.但是在整个项目中实现这一点是不可能的,因为现有列上有完整的流程。

Db structure数据库结构

when tryng to access the permissions of role:当尝试访问角色的权限时:

$role->permissions

It returns the current db value and that make checking permission impossible.它返回当前的数据库值,这使得检查权限变得不可能。 Can anyone please help me how we canoverride the function in the library of any workaround this.谁能帮我解决这个问题,我们如何才能覆盖库中的 function。

Just change laravel/spatie functions name.只需更改laravel/spatie函数名称。

Step 1: Creating a Trait第 1 步:创建特征

Inside our app directory, let's create a new directory and name it Permissions and create a new file namely HasPermissionsTrait.php .在我们的 app 目录中,让我们创建一个新目录并将其命名为 Permissions 并创建一个名为HasPermissionsTrait.php的新文件。 A nice little trait has been set up to handle user relations.已经设置了一个很好的小特征来处理用户关系。 Back in our User model, just import this trait and we're good to go.回到我们的用户 model,只需导入这个特征,我们就可以使用 go。

app/User.php应用程序/用户.php

namespace App;

use App\Permissions\HasPermissionsTrait;

class User extends Authenticatable
{
    use HasPermissionsTrait; //Import The Trait
}

Now go to HasPermissionsTrait.php and add this to it.现在 go 到HasPermissionsTrait.php并将其添加到其中。

App/Permissions/HasPermissionsTrait.php应用/权限/HasPermissionsTrait.php

public function userRolePermissions()
{
    return $this->belongsToMany(Permission::class,'users_permissions');
}

Now change permissions to userRolePermissions and wherever uses it.现在将permissions更改为userRolePermissions以及使用它的任何地方。 And now we can access using $user->userRolePermissions with Laravel Eloquent User .现在我们可以使用$user->userRolePermissions和 Laravel Eloquent User进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM