Laravel Nova 索引按钮未出现

Laravel Nova index buttons not appearing

我正在构建我的 Laravel Nova 界面,并向每个资源添加必要的字段。但是我注意到 edit/detail/trash 按钮没有出现在我的索引视图中。

是否需要将某些内容添加到我的资源中 class,或者是否与我的控制器的构建方式有关?

这是我的字段方法的样子:

/**
 * Get the fields displayed by the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        Gravatar::make(),

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

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

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

        Text::make('Administrator', 'is_admin')
            ->sortable()
            ->rules('required', 'max:255'),

        Password::make('Password')
            ->onlyOnForms()
            ->creationRules('required', 'string', 'min:6')
            ->updateRules('nullable', 'string', 'min:6'),

        HasMany::make('Configuration'),
    ];
}

由于您为用户设置了政策,因此您必须在 UserPolicy class 中包含以下功能:

viewAny
view
create
update
delete
restore
forceDelete

您可以将它们设置为 return true,它们就会出现。例如:

public function update(User $user){
    return true;
}

然后您的更新按钮就会出现。

If a policy exists but is missing a method for a particular action, the user will not be allowed to perform that action. So, if you have defined a policy, don't forget to define all of its relevant authorization methods.

有关政策的更多信息:https://nova.laravel.com/docs/1.0/resources/authorization.html#policies

您可能希望自己而不是其他人制作所有这些 editable,因此您想引入管理功能。

为您的用户添加一个 is_admin 布尔值 table,默认为 0。

Schema::table('users', function($table) {
    $table->boolean('is_admin')->nullable()->default(false);
});

接下来,为您希望管理员能够编辑的每个资源设置策略。 https://laravel.com/docs/5.6/authorization#creating-policies

然后为每个视图添加策略,例如创建、更新、编辑、删除这将允许管理员或授权用户更新用户。

public function update(User $user, User $userBeingEdited)
{
    return $user->is_admin === 1 or $user->id === $userBeingEdited->id;
}

同样,这将只允许您的管理员创建用户

public function create(User $user)
{
    return $user->is_admin === 1;
}

Nova 会自动使用这些按钮,因此只需刷新您的页面,您就会在每一行看到这些按钮。