使用Zizaco的`name`字段的自定义名称/委托包

时间:2022-10-30 10:11:54

I am working in a project that has a large postgreSQL database. The previous project was developed in Java from scratch. We are now developing that in Laravel. The previous system had user management system similar to Zizaco/entrust. So, we used in our system as well. The previous table had module table instead of permission table used in entrust. We have already configured that by changing the table name in config/entrust.php. However, the previous system has permission_name instead of name field used in entrust. How do I config entrust to use the unique permission_name instead of name field.

我正在一个拥有大型postgreSQL数据库的项目中工作。之前的项目是从头开始用Java开发的。我们现在正在Laravel开发它。以前的系统具有类似于Zizaco /委托的用户管理系统。所以,我们也在我们的系统中使用过。上表有模块表而不是委托中使用的权限表。我们已经通过更改config / entrust.php中的表名来配置它。但是,先前的系统具有permission_name而不是委托中使用的名称字段。如何配置委托使用唯一的permission_name而不是name字段。

I am looking for a solution, so that we don't have to change in the sources of entrust because then upgrading it would break the system. Can it be configured in the model?

我正在寻找一个解决方案,这样我们就不必改变委托的来源,因为升级它会破坏系统。可以在模型中配置吗?

1 个解决方案

#1


The Entrust package is hardcoded to use the name attribute, so there is no configuration value or anything to change that. However, one thing you can attempt is to define an accessor and mutator for the name attribute.

Entrust包被硬编码以使用name属性,因此没有配置值或任何可以更改它的内容。但是,您可以尝试的一件事是为name属性定义一个accessor和mutator。

In your App\Permission model, define the following functions:

在App \ Permission模型中,定义以下函数:

class Permission extends Model {
    // accessor
    public function getNameAttribute($value) {
        return $this->permission_name;
    }

    // mutator
    public function setNameAttribute($value) {
        $this->attributes['permission_name'] = $value;
    }
}

Documentation for accessors and mutators: http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

访问者和变更者的文档:http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

#1


The Entrust package is hardcoded to use the name attribute, so there is no configuration value or anything to change that. However, one thing you can attempt is to define an accessor and mutator for the name attribute.

Entrust包被硬编码以使用name属性,因此没有配置值或任何可以更改它的内容。但是,您可以尝试的一件事是为name属性定义一个accessor和mutator。

In your App\Permission model, define the following functions:

在App \ Permission模型中,定义以下函数:

class Permission extends Model {
    // accessor
    public function getNameAttribute($value) {
        return $this->permission_name;
    }

    // mutator
    public function setNameAttribute($value) {
        $this->attributes['permission_name'] = $value;
    }
}

Documentation for accessors and mutators: http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

访问者和变更者的文档:http://laravel.com/docs/5.0/eloquent#accessors-and-mutators