laravel nova hide edit button on index page
Asked Answered
S

7

3

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?

Sack answered 19/6, 2019 at 7:42 Comment(0)
S
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.

Sack answered 5/7, 2019 at 13:36 Comment(7)
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.Orotund
@Max please check the question, it is mentioned to disable in index page and allow in details page. Hiding is not same as disabling.Sack
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.Orotund
@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.Sack
@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.Sack
There’s no index method, or show method. Even your answer doesn’t talk about index or show method.Orotund
@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.Sack
D
9

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.

Dipole answered 23/2, 2021 at 15:45 Comment(2)
You can just add: return false;.Parasitology
Thanks this works for me only in index page.Avenue
B
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.

Blab answered 27/11, 2019 at 1:39 Comment(0)
S
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.

Sack answered 5/7, 2019 at 13:36 Comment(7)
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.Orotund
@Max please check the question, it is mentioned to disable in index page and allow in details page. Hiding is not same as disabling.Sack
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.Orotund
@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.Sack
@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.Sack
There’s no index method, or show method. Even your answer doesn’t talk about index or show method.Orotund
@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.Sack
P
3

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;
}
Parchment answered 29/3, 2021 at 18:46 Comment(1)
Thanks this works for me in index and details page it's the solution I was looking for.Avenue
H
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

Heatherheatherly answered 19/6, 2019 at 9:10 Comment(10)
Thank you for your answer, but I was expecting, if we can set some property and get it done.Sack
There is not any provision for the action items but you can do this for the fields :)Heatherheatherly
okay, thank you for pointing out that. In Nova 2.0 too, they are is no such option or you are talking about 1.0 only?Sack
It's there for 2.0 as well nova.laravel.com/docs/2.0/actions/…Heatherheatherly
I think you got my last question wrong, I was asking in 2.0, too, we can not disable / enable visibility on edit/delete ?Sack
Let us continue this discussion in chat.Sack
Yes.. in nova 2.0 you can't set the visibility for the predefined actions like your requirements.Heatherheatherly
ok, thank you, have they mentioned that anywhere or you found it during your work? any way a great point for documentation if not mentioned .Sack
I'm checking documentation update.. github.com/laravel/nova-docs/blob/develop/2.0/actionsHeatherheatherly
You can do it by creating policy and returning false on update() and adding authorizable() on resource class.Sack
L
2

There is an alternative just using css.

    div[dusk$="-index-component"] table td.td-fit {
     display: none !important;
    }
Loney answered 18/10, 2019 at 19:24 Comment(1)
I want to hide just a view button, how can I do that?Sodalite
O
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.

Orotund answered 4/3, 2020 at 23:43 Comment(2)
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? ThanksSodalite
@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.Orotund

© 2022 - 2024 — McMap. All rights reserved.