Laravel Nova只向超级管理员显示字段

时间:2019-05-08 16:25:47

标签: laravel-5 laravel-nova

您好,我的用户模型有此代码。

const ROLE_SUPER_ADMIN = "SUPER_ADMIN";
public function isSuperAdmin(){
        return $this->hasRole(self::ROLE_SUPER_ADMIN);
}

并且角色表具有“ SUPER_ADMIN”

我为此用户表创建了一个资源,

public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Gravatar::make(),

            Text::make(__('Name'),'name')
                ->sortable()
                ->rules('required', 'max:255'),

            Text::make(__('Surname'),'surname')
                ->sortable()
                ->rules('required', 'max:255'),

            Text::make(__('Mobile token'),'mobile_token'),

            Text::make(__('Email'))
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            Password::make(__('Password'))
                ->onlyOnForms()
                ->creationRules('required', 'string', 'min:6')
                ->updateRules('nullable', 'string', 'min:6'),
            //BelongsToMany::make(__("Businesses"),'businesses',Business::class),
            MorphToMany::make(__('Roles'), 'roles', \Vyuldashev\NovaPermission\Role::class),
            MorphToMany::make(__('Permissions'), 'permissions', \Vyuldashev\NovaPermission\Permission::class),
        ];

我需要的是仅向 SUPER_ADMIN 用户显示此Text::make(__('Mobile token'),'mobile_token'),字段。等领域无限制。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:3)

您可以这样做,

use App\Models\User as UserModel;


Text::make(__('Mobile token'),'mobile_token')
                ->canSee(function ($request) {
                    return $request->user()->hasRole(UserModel::ROLE_SUPER_ADMIN);
                }),