Laravel和Spatie和用户表相互冲突的权限方法

发布于 2025-01-19 08:03:38 字数 383 浏览 3 评论 0原文

我们将Laravel Spatie权限作为文库来管理角色和权限。不幸的是,在实施此库之前,我们在用户表中有一个列作为管理权限的权限。但是现在,当我们实施此库时,它正在与库相抵触。我们尝试重命名为图书馆工作正常的现有列。但是,由于现有列上有全流量,因此在完整项目上实施此功能是不可能的。

db结构

当tryng访问角色权限

$role->permissions

时检查权限不可能。 任何人都可以帮助我如何在任何解决方法的库中canoverrry canoverrry。

We are using laravel spatie permissions as the library to manage the role and permissions. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2025-01-26 08:03:38

只需更改 laravel/spatie/spatie 函数名称。

步骤1:在我们的应用程序目录中创建一个性状

,让我们创建一个新的目录并命名其权限,并创建一个新文件,即haspermissionstrait.php。已经建立了一个很好的小特质来处理用户关系。回到我们的用户模型中,只需导入这个特征,我们就可以了。

app/user.php

namespace App;

use App\Permissions\HasPermissionsTrait;

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

现在转到haspermissionstrait.php,然后将其添加到其中。

app/permissions/haspermissionstrait.php

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

现在将权限>更改为 userrolepermissions和在任何地方使用它。
现在,我们可以使用$ user-> userrololepermissions使用Laravel Eloquent 用户访问。

Just change laravel/spatie functions name.

Step 1: Creating a Trait

Inside our app directory, let’s create a new directory and name it Permissions and create a new file namely 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.

app/User.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.

App/Permissions/HasPermissionsTrait.php

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

Now change permissions to userRolePermissions and wherever uses it.
And now we can access using $user->userRolePermissions with Laravel Eloquent User.

心碎无痕… 2025-01-26 08:03:38

在先决条件部分,
在安装说明中,您将看到必须将 HasRoles 特征添加到用户模型中才能启用此包的功能。

因此,典型的基本用户模型将具有以下基本的最低要求:

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
use HasRoles;

// ..
}

In Prerequisites Section ,
In the Installation instructions you'll see that the HasRoles trait must be added to the User model to enable this package's features.

Thus, a typical basic User model would have these basic minimum requirements:

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
use HasRoles;

// ..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文