Registering Actions
Registering actions in Nova.
Once you have defined an action, you are ready to attach it to a resource. Each resource generated by Nova contains an actions
method. To attach an action to a resource, you should simply add it to the array of actions returned by this method:
Alternatively, you may use the make
method to instantiate your action. Any arguments passed to the make
method will be passed to the constructor of your action:
Authorization
If you would like to only expose a given action to certain users, you may invoke the canSee
method when registering your action. The canSee
method accepts a closure which should return true
or false
. The closure will receive the incoming HTTP request:
You may also use a variety of request methods to get the currently selected resources:
Method | Return Type | Description |
---|---|---|
allResourcesSelected | bool | Returns true if “Select all” selected. |
selectedResourceIds | \Illuminate\Support\Collection|null | Returns null if “Select all” selected or returns a collection of selected resource IDs. |
selectedResources | \Illuminate\Support\Collection|null | Returns null if “Select all” selected or returns a collection of resource models. |
Resource Specific Authorization
Sometimes a user may be able to “see” that an action exists but only “run” that action against certain resources. You may use the canRun
method in conjunction with the canSee
method to have full control over authorization in this scenario. The callback passed to the canRun
method receives the incoming HTTP request as well as the model the user would like to run the action against:
Authorization via Resource Policy
In addition to the canSee
and canRun
authorization methods, Nova will also determine if the resource’s corresponding model policy has runAction
and runDestructiveAction
methods. Finally, Nova will determine if the user is authorized to update
the model or, in the case of destructive actions, delete
the model based on the model’s policy methods.
The priority for authorizing the execution of a Nova action is best explained by the following list of steps:
- Use the return value of the action’s
canRun
method if the method is defined. - Use the return value of the underlying model policy’s
runAction
orrunDestructiveAction
methods if those methods have been defined. - Use the return value of the underlying model policy’s
update
ordelete
methods if those methods have been defined. - Otherwise, return
false
.
Action Visibility
By default, actions are visible on both the resource index and detail pages. However, you may customize an action’s visibility by invoking one of the following methods on the action when registering your action with a particular resource:
onlyOnIndex
exceptOnIndex
showOnIndex
onlyOnDetail
exceptOnDetail
showOnDetail
onlyInline
exceptInline
showInline
Inline Actions
Inline actions are actions that are displayed directly on the index table row of a given resource. You may specify that an action should be available inline by calling the showInline
method when attaching the action to the resource:
Standalone Actions
Typically, actions are executed against resources selected on a resource index or detail page. However, sometimes you may have an action that does not require any resources / models to run. In these situations, you may register the action as a “standalone” action by invoking the standalone
method when registering the action. These actions always receives an empty collection of models in their handle
method:
Sole Actions
Sometimes you may have actions that should only ever be run on a single resource / model. By registering the action as a sole
action, Nova will only display the action when a single resource is selected. Sole actions still receive a collection in their handle
method, but the collection will only contain a single model:
Pivot Actions
Typically, actions operate on a resource. However, you may also attach actions to belongsToMany
fields so that they can operate on pivot / intermediate table records. To accomplish this, you may chain the actions
method onto your field’s definition:
Once the action has been attached to the field, you will be able to select the action and execute it from the relationship index on the parent resource’s detail page.
Custom Pivot Action Name
By default, the pivot actions within the action dropdown menu will be grouped as “Pivot”, but you may customize this name using the referToPivotAs
method:
Closure Actions
Closure actions allow you to create actions without defining the action as a separate class. To define a closure action, call the using
factory method on the Action
class, passing the action’s name and a closure. The closure given to the using
method receives the same parameters as a dedicated action’s handle
method:
Closure actions are not queueable since they cannot use the ShouldQueue
trait provided by Laravel.
Static Actions
When using Nova, it’s common to define actions to accomplish simple tasks like downloading files, redirecting users, or opening new windows. Luckily, Nova provides static actions, allowing you to accomplish a variety of common tasks without writing a dedicated action of your own.
Redirect Actions
The redirect
action will redirect the user to an external URL. To create a redirect
action, pass the action name and the URL you would like to redirect the user to:
Visit Actions
The visit
action will push the user to an internal page inside Nova. To create a visit
action, pass the action’s name and the path you want them to visit:
Danger Actions
The danger
action displays an error toast notification to the user. For instance, your Nova application may have an action that was previously available but is no longer available, and to avoid confusion you may wish to notify the user of its removal. To accomplish this, pass the action name and the message to display to the user:
Custom Modal Actions
The modal
action allows you to display a custom modal to the user. To create a modal
action, pass the action name, your custom Vue component, and any additional data that should be made available to the component:
Open URLs in New Tabs
The openInNewTab
action opens a URL in a new browser tab. To create an openInNewTab
action, pass the action name and the URL that should be opened in the new browser tab:
You may also configure the URL to be unique for a resource by defining a Sole Action:
Downloading Files
The downloadUrl
action downloads the file at the given URL. To create a downloadUrl
action, pass the action name and the URL of the file to be downloaded:
Action Confirmation Modal
Fullscreen / Custom Modal Sizes
When running an action, a confirmation modal is typically displayed to the user, allowing them an opportunity to cancel the pending operation. To indicate that the confirmation modal should display as fullscreen, you may invoke the fullscreen
method when registering your action with a given resource:
Alternatively, you may further customize the maximum width of the customization modal using the size
method:
Disabling Action Confirmation
To disable the action confirmation modal and therefore run actions immediately, you can invoke the withoutConfirmation
method when registering your action with a given resource:
Was this page helpful?