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 class in app/Admin/Buttons/ChangeOrderStatusButton.php. Subdirectories in the name become namespaces, admin:button Orders/SendInvoice creates App\Admin\Buttons\Orders\SendInvoice.

Registering a button

Assign the button to an admin model through the $buttons property. Buttons can also be returned by a buttons() method, or extended with setButtonsProperty($buttons).
app/Models/Order.php
Once registered, the button appears in the side panel of every record.
Custom button in the record side panel
A button that can run over several records is placed in the action bar once records are checked.
Custom action in the action bar above selected records

Configuring the button

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

Type

Where the button is available depends on its type and on the methods it defines. The default type is button.

Class and colour

A Bootstrap button class: btn-default, btn-primary, btn-success, btn-warning, btn-danger.

Running the action

fire runs the action over a single record clicked in the side panel. fireMultiple runs over the checked records. Both return a response in the form of a message or your own component.
app/Admin/Buttons/ChangeOrderStatusButton.php
In __construct you can change the button for the record passed to it. $active = false hides the button for that record, and the server refuses to run it for the record.

Message title and text

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

Redirect

A RedirectResponse returned from fire() works the same way:

New window

Open a record

open() with a model opens that record in a modal instead of a URL.

Custom Vue component

Create the component with php artisan admin:component OrderStats and choose the button component type. Components are resolved as described in component directories, e.g. component('orders/OrderStats.vue') for a subdirectory.

Actions with a confirmation

The question method receives the first request and returns a confirmation 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/PostponeDelivery.php
resources/views/admin/components/buttons/PostponeDelivery.vue
The second argument of component($component, $data) passes data available in the Vue component through the data prop.

Multi-step actions

action('method') in a response makes the next confirmation call that method of the button.
app/Admin/Buttons/SyncOrdersButton.php