Skip to main content
Glama
brianirish

Laravel 12 Docs MCP Server

by brianirish
registering-actions.md10.2 kB
# Nova - Actions/Registering-Actions *Source: https://nova.laravel.com/docs/v5/actions/registering-actions* --- - [Community](https://discord.com/invite/laravel) - [Blog](https://blog.laravel.com) ##### Get Started - [Installation](/docs/v5/installation) - [Release Notes](/docs/v5/releases) - [Upgrade Guide](/docs/v5/upgrade) ##### Resources - [The Basics](/docs/v5/resources/the-basics) - [Fields](/docs/v5/resources/fields) - [Dependent Fields](/docs/v5/resources/dependent-fields) - [Date Fields](/docs/v5/resources/date-fields) - [File Fields](/docs/v5/resources/file-fields) - [Repeater Fields](/docs/v5/resources/repeater-fields) - [Field Panels](/docs/v5/resources/panels) - [Relationships](/docs/v5/resources/relationships) - [Validation](/docs/v5/resources/validation) - [Authorization](/docs/v5/resources/authorization) ##### Search - [The Basics](/docs/v5/search/the-basics) - [Global Search](/docs/v5/search/global-search) - [Scout Integration](/docs/v5/search/scout-integration) ##### Filters - [Defining Filters](/docs/v5/filters/defining-filters) - [Registering Filters](/docs/v5/filters/registering-filters) ##### Lenses - [Defining Lenses](/docs/v5/lenses/defining-lenses) - [Registering Lenses](/docs/v5/lenses/registering-lenses) ##### Actions - [Defining Actions](/docs/v5/actions/defining-actions) - [Registering Actions](/docs/v5/actions/registering-actions) ##### Metrics - [Defining Metrics](/docs/v5/metrics/defining-metrics) - [Registering Metrics](/docs/v5/metrics/registering-metrics) ##### Digging Deeper - [Dashboards](/docs/v5/customization/dashboards) - [Menus](/docs/v5/customization/menus) - [Notifications](/docs/v5/customization/notifications) - [Authentication](/docs/v5/customization/authentication) - [Impersonation](/docs/v5/customization/impersonation) - [Tools](/docs/v5/customization/tools) - [Resource Tools](/docs/v5/customization/resource-tools) - [Cards](/docs/v5/customization/cards) - [Fields](/docs/v5/customization/fields) - [Filters](/docs/v5/customization/filters) - [CSS / JavaScript](/docs/v5/customization/frontend) - [Assets](/docs/v5/customization/assets) - [Localization](/docs/v5/customization/localization) - [Stubs](/docs/v5/customization/stubs) On this page - [Authorization](#authorization) - [Resource Specific Authorization](#resource-specific-authorization) - [Authorization via Resource Policy](#authorization-via-resource-policy) - [Action Visibility](#action-visibility) - [Inline Actions](#inline-actions) - [Standalone Actions](#standalone-actions) - [Sole Actions](#sole-actions) - [Pivot Actions](#pivot-actions) - [Custom Pivot Action Name](#custom-pivot-action-name) - [Closure Actions](#closure-actions) - [Static Actions](#static-actions) - [Redirect Actions](#redirect-actions) - [Visit Actions](#visit-actions) - [Danger Actions](#danger-actions) - [Custom Modal Actions](#custom-modal-actions) - [Open URLs in New Tabs](#open-urls-in-new-tabs) - [Downloading Files](#downloading-files) - [Action Confirmation Modal](#action-confirmation-modal) - [Fullscreen / Custom Modal Sizes](#fullscreen-%2F-custom-modal-sizes) - [Disabling Action Confirmation](#disabling-action-confirmation) Actions # 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: Construct Make Copy Ask AI ``` use App\Nova\Actions\EmailAccountProfile; use Laravel\Nova\Http\Requests\NovaRequest; // ... /** * Get the actions available for the resource. * * @return array<int, \Laravel\Nova\Actions\Action> */ public function actions(NovaRequest $request): array { return [ new Actions\EmailAccountProfile, ]; } ``` ## [​](#authorization) 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: app/Nova/~Resource.php Copy Ask AI ``` use App\Models\User as UserModel; use Laravel\Nova\Http\Requests\NovaRequest; // ... /** * Get the actions available for the resource. * * @return array<int, \Laravel\Nova\Actions\Action> */ public function actions(NovaRequest $request): array { return [ Actions\EmailAccountProfile::make() ->canSee(function ($request) { return $request->user()->can( 'emailAnyAccountProfile', UserModel::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. | ### [​](#resource-specific-authorization) 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: app/Nova/~Resource.php Copy Ask AI ``` use Laravel\Nova\Http\Requests\NovaRequest; // ... /** * Get the actions available for the resource. * * @return array<int, \Laravel\Nova\Actions\Action> */ public function actions(NovaRequest $request): array { return [ Actions\EmailAccountProfile::make() ->canSee(function ($request) { return true; })->canRun(function ($request, $user) { return $request->user()->can('emailAccountProfile', $user); }), ]; } ``` ### [​](#authorization-via-resource-policy) 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: 1. Use the return value of the action’s `canRun` method if the method is defined. 2. Use the return value of the underlying model policy’s `runAction` or `runDestructiveAction` methods if those methods have been defined. 3. Use the return value of the underlying model policy’s `update` or `delete` methods if those methods have been defined. 4. Otherwise, return `false`. ## [​](#action-visibility) 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 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: app/Nova/~Resource.php Copy Ask AI ``` use Laravel\Nova\Http\Requests\NovaRequest; // ... /** * Get the actions available for the resource. * * @return array<int, \Laravel\Nova\Actions\Action> */ public function actions(NovaRequest $request): array { return [ Actions\ConsolidateTransaction::make() ->showInline(), ]; } ``` ## [​](#standalone-actions) 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: app/Nova/~Resource.php Copy Ask AI ``` use Laravel\Nova\Http\Requests\NovaRequest; // ... /** * Get the actions available for the resource. * * @return array<int, \Laravel\Nova\Actions\Action> */ public function actions(NovaRequest $request): array { return [ Actions\InviteUser::make() ->standalone(), ]; } ``` ## [​](#sole-actions) 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: app/Nova/~Resource.php Copy Ask AI ``` use Laravel\Nova\Http\Requests\NovaRequest; // ... /** * Get the actions available for the resource. * * @return array<int, \Laravel\Nova\Actions\Action> */ public function actions(NovaRequest $request): array { return [ Actions\BanUser::make() ->sole(), ]; } ``` ## [​](#pivot-actions) 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: app/Nova/~Resource.php Copy Ask AI ``` use La *[Content truncated for length]*

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/brianirish/laravel-docs-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server