Laravel Spatie权限-如何通过其他模型更改默认模型“角色”

时间:2020-06-22 14:54:43

标签: laravel laravel-permission

请在我的项目(laravel项目)中使用另一个模型来更改默认模型“角色”,该模型具有与“角色”相同的体系结构;默认模型包。

config / permission.php 配置文件中的

包含:

'models' => [

    /*
     * When using the "HasPermissions" trait from this package, we need to know which
     * Eloquent model should be used to retrieve your permissions. Of course, it
     * is often just the "Permission" model but you may use whatever you like.
     *
     * The model you want to use as a Permission model needs to implement the
     * `Spatie\Permission\Contracts\Permission` contract.
     */

    'permission' => Spatie\Permission\Models\Permission::class,

    /*
     * When using the "HasRoles" trait from this package, we need to know which
     * Eloquent model should be used to retrieve your roles. Of course, it
     * is often just the "Role" model but you may use whatever you like.
     *
     * The model you want to use as a Role model needs to implement the
     * `Spatie\Permission\Contracts\Role` contract.
     */

    'role' => Spatie\Permission\Models\Role::class,

],

我想要这样的东西:

'role' => App\Fonction::class,

文档说我必须实施 Spatie \ Permission \ Contracts \ Role`合约。 不知道我该如何以正确的方式做到这一点。

功能模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Fonction extends Model
{

    protected $fillable = [
        'nom','description'
    ];


    public function comptes()
    {
        return $this->hasMany('App\Compte') ;
    }

}

1 个答案:

答案 0 :(得分:0)

您可以扩展Spatie\Permission\Models\Role类。它已经实现了Spatie\Permission\Contracts\Role接口。在doc中查看详细信息。

<?php

namespace App;

use Spatie\Permission\Models\Role;

class Fonction extends Role
{

    protected $fillable = [
        'nom','description'
    ];


    public function comptes()
    {
        return $this->hasMany('App\Compte') ;
    }

}