# Registering Actions
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 \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
new Actions\EmailAccountProfile
];
}
Alternatively, you may use the make
method to instantiate your action:
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
Actions\EmailAccountProfile::make()
];
}
Any arguments passed to the make
method will be passed to the constructor of your action.
# Disabling Action Confirmation
By default, when running an action a confirmation modal is displayed to the user, allowing them to cancel the pending operation. To disable this (and run the action immediately), you can chain the withoutConfirmation
method to your action definition:
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
Actions\EmailAccountProfile::make()->withoutConfirmation()
];
}
# Action Visibility
By default, actions are visible on both the resource index and detail pages. In addition, inline actions are hidden from the table row's actions dropdown by default. You may designate an action's visibility by setting one of the following methods on the action when registering it:
onlyOnIndex
exceptOnIndex
showOnIndex
onlyOnDetail
exceptOnDetail
showOnDetail
onlyOnTableRow
exceptOnTableRow
showOnTableRow
# Inline Actions
Inline actions are actions that are displayed as buttons directly on the index table row a given resource. You may specify that an action should be available inline by calling the showOnTableRow
method when attaching the action to the resource:
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
(new ConsolidateTransaction())->showOnTableRow()
];
}
# Standalone Actions
Typically, actions executed against on selected resources from a resource index or from a resource's detail page. If you have an action that does not require any resources / models to run, you may register it as a "standalone" action by chaining the standalone
method when registering the action. This action always receives an empty collection of models in its handle
method:
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
Actions\InviteUser::make()->standalone()
];
}
# Authorization
If you would like to only expose a given action to certain users, you may chain the canSee
method onto your action registration. The canSee
method accepts a Closure which should return true
or false
. The Closure will receive the incoming HTTP request:
use App\User;
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
(new Actions\EmailAccountProfile)->canSee(function ($request) {
return $request->user()->can(
'emailAnyAccountProfile', User::class
);
}),
];
}
Resource Action Model Resolution
It's important to remember that Resource
actions are not always resolved using an underlying Model
instance. Because of this, you should check for the existence of the model instead of assuming one is available.
# Authorizing Actions Per-Resource
Sometimes it is useful to conditionally display an action based on some state in the resource's underlying model. To do this, you can retrieve the resource via the resource
property on a resource or lens instance:
use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Http\Requests\ActionRequest;
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
(new Actions\CancelTrial)->canSee(function ($request) {
if ($request instanceof ActionRequest) {
return true;
}
return $this->resource instanceof Model && $this->resource->isOnTrial();
}),
];
}
# The canRun
Method
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 \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
(new Actions\EmailAccountProfile)->canSee(function ($request) {
return true;
})->canRun(function ($request, $user) {
return $request->user()->can('emailAccountProfile', $user);
}),
];
}
# 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:
BelongsToMany::make('Roles')
->actions(function () {
return [
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's resource detail page.