Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

laravel nova hide edit button on index page

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?

Upvotes: 4

Views: 15832

Answers (8)

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

In your resource, add the below function:

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

To disable delete, add the below function:

public function authorizedToDelete(Request $request)
{
   return false;
}

Upvotes: 0

istavros
istavros

Reputation: 158

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;
}

Upvotes: 4

Adam Hutchison
Adam Hutchison

Reputation: 101

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.

Upvotes: 10

Max S.
Max S.

Reputation: 1461

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.

Upvotes: 1

Grant
Grant

Reputation: 6329

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.

Upvotes: 4

João Santos
João Santos

Reputation: 43

There is an alternative just using css.

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

Upvotes: 2

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

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.

Upvotes: 3

Vikash Pathak
Vikash Pathak

Reputation: 3562

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

Upvotes: 2

Related Questions