问题
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?
回答1:
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:
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:
You can define the custom actions and set the action visibility as per your requirements.
- Create New Action Class:
# To generate the action class
php artisan nova:action DeleteUserData --destructive
- 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
回答3:
There is an alternative just using css.
div[dusk$="-index-component"] table td.td-fit {
display: none !important;
}
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/56662505/laravel-nova-hide-edit-button-on-index-page