2

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?

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105

7 Answers7

5

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.

4

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.

Grant
  • 5,709
  • 2
  • 38
  • 50
3

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.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • It will hide the button, but it'll also disable any custom operations, in this case under the `edit` authorization policy. That's not what the user asked. The user asked to keep the policy but hide the button only. – Max S. Mar 04 '20 at 23:30
  • @Max please check the question, it is mentioned to disable in index page and allow in details page. Hiding is not same as disabling. – Prafulla Kumar Sahu Mar 05 '20 at 00:42
  • I checked the question. You should check the question. He wants to remove the buttons from index without disabling the operations. Your answer will not work, because it will disable ‘edit’ everywhere, even in detail view, and allow all other operations. – Max S. Mar 05 '20 at 04:14
  • 4
    @Max I am the person who has asked the question, so I am very clear about the requirement. I hope your answer too helpful for others and yourself. My answer was exactly what I wanted before posting this question. – Prafulla Kumar Sahu Mar 05 '20 at 06:26
  • @Max 1st let me clear you, for this CSS is never ever ever a solution, in policy class return false in index method and true in show method, will disable buttons in index page and allow it on detail page. By the way I have more than 6000 reputation, that means I have something, I guess. – Prafulla Kumar Sahu Mar 05 '20 at 12:31
  • There’s no index method, or show method. Even your answer doesn’t talk about index or show method. – Max S. Mar 05 '20 at 17:04
  • @max I don’t think people who works in Laravel will need Policy class here, they will go through documentation to learn details of Policies instead and if they know, this answer should be enough to understand how it works with Nova resource. – Prafulla Kumar Sahu Mar 05 '20 at 17:07
2

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

Vikash Pathak
  • 3,444
  • 1
  • 18
  • 32
2

There is an alternative just using css.

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

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;
}
istavros
  • 138
  • 5
1

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.

Max S.
  • 1,383
  • 14
  • 25
  • This is what I was looking for, but I tried to change -delete- to -view- and it does not work? How can I remove just a view button instead of a delete button? Thanks – Christopher Kikoti Mar 04 '22 at 08:35
  • @ChristopherKikoti It depends on which model you're using it, you'll need to replace `users-index-component`, try inspect the element to see what it's called. – Max S. Mar 04 '22 at 12:28