laravel-permission icon indicating copy to clipboard operation
laravel-permission copied to clipboard

feat(Roles): Support for casting role names to enums

Open gajosadrian opened this issue 1 year ago • 1 comments

fixes #2609

It's more like a new feature because @schnetzi extended base Role model and did:

protected $casts = [
    'name' => UserRoleEnum::class,
];

I used a custom accesor to dynamically cast it into the corresponding enum. Otherwise, it returns the original name attribute value.

/**
 * @return string|\BackedEnum
 */
public function getNameAttribute()
{
    $name = $this->attributes['name'];

    if (str_contains($name, 'casted_enum')) {
        return TestRolePermissionsEnum::from($name);
    }

    return $name;
}

gajosadrian avatar Feb 17 '24 04:02 gajosadrian

Probably need to add tests with string parameters too. Actually I don't know.. for sure it works if the given parameters are enums as well the casts.

gajosadrian avatar Feb 17 '24 04:02 gajosadrian

Thanks!

drbyte avatar Apr 19 '24 03:04 drbyte