5

I am in the process of building out my Laravel Nova interface, and adding the necessary fields to each of the resources. However I am noticing that the edit/detail/trash buttons are not appearing on my index view.

Is there something that needs to be added to my resource class, or does it have to do with how my Controllers are built?

This is what my fields method look like:

/**
 * 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'),
    ];
}

List view image

Doug Niccum
  • 196
  • 4
  • 16

2 Answers2

4

Since you have a policy setup for your users, you must include the following functions in your UserPolicy class:

viewAny
view
create
update
delete
restore
forceDelete

You can set them to return true and they will appear. For example:

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

Then your update button will show up.

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.

For more information about the policies: https://nova.laravel.com/docs/1.0/resources/authorization.html#policies

Chin Leung
  • 14,621
  • 3
  • 34
  • 58
1

You'll probably want to make all these editable by you and no one else, so you want to introduce an admin capability.

Add an is_admin boolean to your users table, default 0.

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

Next, set up policies on each of the resources you want your admin to be able to edit. https://laravel.com/docs/5.6/authorization#creating-policies

Then add policies for each of the view, create, update, edit, delete e.g. this will allow the admin or the authed user to update the user.

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

Similarly, this will allow just your admin to create a user

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

Nova will work with these automatically so just refresh your page and you should see the buttons on each line.

Drewster
  • 499
  • 5
  • 13