![](/img/trans.png)
[英]Laravel Nova - Hide update icon (edit attached) on related resource
[英]laravel nova hide edit button on index page
如何禁用 nova 索引頁面上的編輯/刪除按鈕並仍然允許詳細頁面,如果我將創建一個策略,這將在任何地方禁用該操作,我想允許在詳細頁面中編輯和刪除,但只想刪除那些按鈕從索引,
正在做類似的事情
public function update(User $user, Customer $customer)
{
if ( request()->route()->getName('route-name') ) {
return false;
}
}
是正確的方法還是有更好的方法?
我有一個潛在客戶資源,我需要隱藏其上的編輯按鈕。 我在我的 CSS 中執行了以下操作 - 請參閱此處了解如何將您自己的 CSS 添加到 Nova 。
使用我的Leads資源的slug,我可以通過slug和resource部分來參考dusk屬性:
div[dusk="leads-index-component"] table td.td-fit span:last-of-type {
display: none !important;
}
所以你要改變的部分是{your-resource-slug}-index-component
leads-index-component
部分是{your-resource-slug}-index-component
另外,如果要隱藏視圖和編輯圖標,只需刪除:last-of-type
部分:
作為參考,我正在使用Button Field 包添加自定義按鈕以重定向到我自己的自定義工具來管理此資源。
我不隸屬於所提供的任何鏈接。
您可以定義自定義操作並根據您的要求設置操作可見性。
# To generate the action class
php artisan nova:action DeleteUserData --destructive
/**
* 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;
源代碼: https : //nova.laravel.com/docs/1.0/actions/defining-actions.html#action-visibility
如果您想禁用索引頁面上的任何行按鈕,請為資源創建一個策略,並在我的案例update()
在相應的函數上返回 false,
所有其他人都返回 true 並在 AuthServiceProvider.php add 上添加策略
protected $policies = [
Post::class => PostPolicy::class,
];
並在資源類中
public static function authorizable()
{
return true;
}
這將禁用該按鈕。
還有一種替代方法是使用 css。
div[dusk$="-index-component"] table td.td-fit {
display: none !important;
}
我知道這個線程有點舊,但你也可以從你的 nova 資源中覆蓋authorizedToUpdate
方法,如下所示:
public function authorizedToUpdate(Request $request): bool
{
return "nova-api/{resource}" != $request->route()->uri();
}
這也適用於authorizedToView
和authorizedToDelete
。
似乎只存在一個 CSS 解決方案,例如:
/* 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;
}
輸入您的組件名稱,在本例中為users-
。
其他涉及授權和策略的解決方案不僅會隱藏按鈕,還會完全禁用操作,因此您將無法在需要時使用自定義操作來運行它。
我想做類似的事情。 我不希望編輯按鈕出現在索引頁面上,但我希望操作能夠運行(並更新資源)。 所以我使用了下面的代碼:
use Laravel\Nova\Http\Requests\ActionRequest;
...
public function authorizedToUpdate(Request $request)
{
return $request instanceof ActionRequest;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.