# Registering Actions
Once you have defined an action, you are ready to attach it to a resource. Each resource generated by Nova contains a 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];
}
# 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
);
}),
];
}
# 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.