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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
new Actions\EmailAccountProfile
];
}
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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
Actions\EmailAccountProfile::make()
];
}
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:
use App\Models\User;
use Laravel\Nova\Http\Requests\NovaRequest;
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
(new Actions\EmailAccountProfile)->canSee(function ($request) {
return $request->user()->can(
'emailAnyAccountProfile', User::class
);
}),
];
}
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. |
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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
(new Actions\EmailAccountProfile)->canSee(function ($request) {
return true;
})->canRun(function ($request, $user) {
return $request->user()->can('emailAccountProfile', $user);
}),
];
}
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:
canRun
method if the method is defined.runAction
or runDestructiveAction
methods if those methods have been defined.update
or delete
methods if those methods have been defined.false
.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 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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
(new ConsolidateTransaction())->showInline()
];
}
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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
Actions\InviteUser::make()->standalone()
];
}
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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
Actions\BanUser::make()->sole()
];
}
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:
BelongsToMany::make('Roles')
->actions(fn () => [new Actions\MarkAsActive]),
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.
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:
BelongsToMany::make('Roles')
->actions(fn () => [new Actions\MarkAsActive])
->referToPivotAs('Role Assignment'),
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:
public function actions()
{
return [
Action::using('Deactivate User', function (ActionFields $fields, Collection $models) {
$models->each->update(['active' => false]);
}),
];
}
Queueing Closure Actions
Closure actions are not queueable since they cannot use the ShouldQueue
trait provided by Laravel.
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.
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:
public function actions()
{
return [
Action::redirect('Visit Stripe Dashboard', 'https://stripe.com')->standalone(),
];
}
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:
use Laravel\Nova\Nova;
public function actions()
{
return [
Action::visit('View Logs', Nova::url('/logs'))->standalone(),
];
}
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:
public function actions()
{
return [
Action::danger('Disable User Account', 'This action is no longer available!'),
];
}
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:
public function actions()
{
return [
Action::modal('Download User Summary', 'UserSummary', function ($user) {
return [
'user_id' => $user->getKey(),
];
})->sole(),
];
}
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:
public function actions()
{
return [
Action::openInNewTab('Visit Stripe Dashboard', 'https://stripe.com')->standalone(),
];
}
You may also configure the URL to be unique for a resource by defining a Sole Action:
Action::openInNewTab('Visit User Profile', function ($user) {
return route('user.profile', $user);
})->sole(),
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:
public function actions()
{
return [
Action::downloadUrl('Download Users Summaries', function () {
return route('users.summaries');
})->standalone(),
];
}
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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
Actions\EmailAccountProfile::make()->fullscreen()
];
}
Alternatively, you may further customize the maximum width of the customization modal using the size
method:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
// "sm", "md", "lg", "xl", "2xl", "3xl", "4xl", "5xl", "6xl", or "7xl"...
Actions\EmailAccountProfile::make()->size('7xl')
];
}
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:
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [
Actions\EmailAccountProfile::make()->withoutConfirmation()
];
}