Sometimes a module needs to run an action over a record or a group of records. In an order, for example, you may need to change the order status, which also sends a notification to the courier. Custom buttons cover those cases. They are rendered in the record table, and clicking one sends a request to the server where you can run any action and return a message to the user.

Creating a button

The command creates the button configuration file in app/Admin/Buttons/ChangeOrderStatusButton.php.

Registering a button

Assign the button to an admin model through the $buttons property.
app/Order.php

How the button is rendered

Once registered, the button appears in the side panel of every record.
Custom button in the record side panel
If the button can be applied to several selected records, it is placed in the action bar once records are selected.
Custom action in the action bar above selected records

Configuring the button

A button is a PHP class holding its properties and the method that runs when it is clicked.
app/Admin/Buttons/ChangeOrderStatusButton.php

Type (required)

There are three types of action.
For an action that runs over several records, fire($row) is not enough — it receives a single record. Define fireMultiple($rows) as well, which receives the selected records.

Name (required)

Class and colour

One of fa-default, fa-primary, fa-success, fa-warning, fa-error.

Icon

Running the action

fire runs the action over a single record clicked from the side panel. fireMultiple only needs to be defined for actions that can run over several records. Both return a response in the form of a message or your own component.
app/Admin/Buttons/ChangeOrderStatusButton.php
In __construct you can mutate the default behaviour of the button for the specific record passed to it. Use the active property there to disable the button for a given record.

Message title and text

To return plain text or HTML, use one of message, success, error or warning.

Redirect

New window

Custom Vue or blade component

Create the component with php artisan admin:component MyComponentName and choose the button component type.
Components are loaded recursively from resources/views/admin/components. The directory is configurable in the administration configuration. For a component in another location, use an absolute path.
With a larger number of components, use subdirectories. Call a component from a subdirectory by its name, or by its relative path — component('subdirectory/MyComponentName.vue').

Actions with a confirmation

Sometimes the user has to be warned before an action runs. The question method receives the confirmation request first and returns the confirmation response as a message or a component. Once confirmed, fire or fireMultiple runs. The example below asks a question through a component with an input, whose value is then sent to fire.
app/Admin/Buttons/MyButton.php
resources/views/admin/components/FillAgeComponent.vue
app/Admin/Buttons/MyButton.php
The second argument of component($component, $data) passes data that is available in the Vue component through the data prop.