1
votes

How to disable edit/delete button on nova index page and still allow in detail page, if I will create a policy, that will disable the operation everywhere, I want to allow edit and delete in detail page, but just want to remove those button from index,

is doing something like

 public function update(User $user, Customer $customer)
    {
        if ( request()->route()->getName('route-name') ) {
            return false;
        }
    }

is correct way or there is any better way?

7

7 Answers

3
votes

I had a resource of Leads and I needed to hide the edit button on it. I did the following, in my CSS - see here for how to add your own CSS to Nova.

Using the slug of my Leads resource, I can refer to the dusk attribute by slug and resource section:

div[dusk="leads-index-component"] table td.td-fit span:last-of-type {
    display: none !important;
}

So the part you'd change is the leads-index-component part to be {your-resource-slug}-index-component

Also, just remove the :last-of-type part if you want to hide both the view and the edit icon:

enter image description here

For reference, I am using the Button Field package to add the custom button to redirect to my own custom tool for management of this resource.

I am not affiliated with any of the links provided.

2
votes

You can define the custom actions and set the action visibility as per your requirements.

  1. Create New Action Class:
# To generate the action class
php artisan nova:action DeleteUserData --destructive
  1. Set Action Visibility:
/**
 * Indicates if this action is only available on the resource index view.
 *
 * @var bool
 */
public $onlyOnIndex = false;

/**
 * Indicates if this action is only available on the resource detail view.
 *
 * @var bool
 */
public $onlyOnDetail = true;

Src: https://nova.laravel.com/docs/1.0/actions/defining-actions.html#action-visibility

2
votes

If you want to disable any row button on index page, create a policy for the resource and return false on the respective function in my case update(),

all others return true and add the policy on AuthServiceProvider.php add

protected $policies = [
    Post::class => PostPolicy::class,
];

and in Resource class

public static function authorizable()
{
    return true;
}

that will disable that button.

2
votes

There is an alternative just using css.

    div[dusk$="-index-component"] table td.td-fit {
     display: none !important;
    }
1
votes

I know this thread is a little old but you can also override the authorizedToUpdate method from within your nova resource like so:

public function authorizedToUpdate(Request $request): bool
{
    return "nova-api/{resource}" != $request->route()->uri();
}

This also works for authorizedToView and authorizedToDelete.

0
votes

Only a CSS solution seems to exist, such as:

/* Details page */
div[dusk="users-detail-component"] button[dusk="open-delete-modal-button"],
/* Index page next to each row */
div[dusk="users-index-component"] button[dusk$="-delete-button"],
/* Index page after checking boxes */
div[dusk="users-index-component"] div[dusk="delete-menu"] {
  display: none !important;
}

Enter your components name, in this case it's users-.

Other solutions that involve authorization and policies will not only hide button, but disable the action completely, so you won't be able to run it with a custom action if needed.

0
votes

I wanted to do something similar. I didn't want the edit button to appear at the index page, but I wanted the actions to run (and update the resources). So I used the code below:

use Laravel\Nova\Http\Requests\ActionRequest;

...

public function authorizedToUpdate(Request $request)
{
    return $request instanceof ActionRequest;
}