# 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
            );
        }),
    ];
}

# Authorizing Actions Per-Resource

Sometimes it is useful to conditionally show an action based on some state in the resource's underlying model. To do this you can retrieve the resource from the request using the findModelQuery method found on NovaRequest:

/**
 * 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) {
            return optional($request->findModelQuery()->first())->isOnTrial();
        }),
    ];
}

WARNING

It's important to remember that Resource actions are not always resolved using an underlying Model instance. Because of this, it's important to check for the existence of the model, instead of assuming one is available.

# 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.