Skip to main content

Actions

An Action allows you to add features to a table, like viewing, editing or deleting a row. Anything you can do with PHP you can do with actions. They appear on the right side of the table, tucked behind a dropdown menu.

Creating actions

Add actions by overriding the actions method of a table:

 1namespace App\Tables;
 2
 3use App\Models\ProductReview;
 4use Larasell\Admin\Tables\Actions\Action;
 5use Larasell\Admin\Tables\Table;
 6
 7class ProductReviewTable extends Table
 8{
 9    public function actions(): array
10    {
11        return [
12            Action::make('Publish', fn (ProductReview $record) => $record->publish()),
13        ];
14    }
15}

When a user clicks the action, the code runs for that row.

Icons

Add an icon to an action with the icon method. It accepts an Icon enum case or a Remix icon name:

 1use Larasell\Admin\Enums\Icon;
 2
 3public function actions(): array
 4{
 5    return [
 6        Action::make('Publish', fn (ProductReview $record) => $record->publish())
 7            ->icon(Icon::UploadCloud2),
 8
 9        // The Remix icon name also works as a string
10        Action::make('Delete', fn (ProductReview $record) => $record->delete())
11            ->icon('delete-bin'),
12    ];
13}

The route method turns the action into a link to a named route. When a user clicks it, the link takes them to that page. Without parameters, the record becomes the route parameter. Use it to link to the create or edit page of a model.

1public function actions(): array
2{
3    return [
4        Action::make('View')->route('reviews.show'),
5    ];
6}

You can also link anywhere with the to method:

1public function actions(): array
2{
3    return [
4        // The URL can be a static string or a closure that receives the record
5        Action::make('Guidelines')->to('https://shop.example.test/guidelines'),
6    ];
7}

Destructive actions

Each action you add looks the same. That makes destructive actions like deleting a record look less dangerous than they are. Use the destructive method to give the action a red colour. This is visual.

1public function actions(): array
2{
3    return [
4        Action::make('Delete', fn (ProductReview $record) => $record->delete())
5            ->destructive(),
6    ];
7}

Confirmation

For destructive actions like deleting a record, show a confirmation dialog the user must complete. That reduces the chance of them doing an irreversible action.

An Action accepts a confirm method:

1public function actions(): array
2{
3    return [
4        Action::make('Delete', fn (ProductReview $record) => $record->delete())
5            ->confirm(),
6    ];
7}

Customising the confirmation dialog

The confirm method with no parameters is enough. Customise what the user sees:

 1public function actions(): array
 2{
 3    return [
 4        Action::make('Delete', fn (ProductReview $record) => $record->delete())
 5            ->confirm(
 6                title: 'Delete review',
 7                message: 'This review will be gone forever.',
 8                confirmButton: 'Delete review',
 9                cancelButton: 'Keep review',
10            ),
11    ];
12}

All arguments are optional.

Type to confirm

For some destructive actions, require the user to type some text to confirm their action.

1public function actions(): array
2{
3    return [
4        Action::make('Delete', fn (ProductReview $record) => $record->delete())
5            ->confirm(confirmationText: 'delete'),
6    ];
7}

When the user tries to delete a product review, they're forced to type "delete" into an input field. They cannot perform the action if what they've entered does not match the required text.

You can make the required text depend on the record:

1public function actions(): array
2{
3    return [
4        Action::make('Delete', fn (ProductReview $record) => $record->delete())
5            ->confirm(confirmationText: fn (ProductReview $record) => $record->author),
6    ];
7}

The user has to type the author's name to confirm their action.

Disabling actions

A user can't perform every action. They don't have enough permissions, or they want to remove a user but they can't remove themselves.

To disable actions use the disabled method:

1public function actions(): array
2{
3    return [
4        Action::make('Publish', fn (ProductReview $record) => $record->publish())
5            ->disabled(fn (ProductReview $record) => $record->is_published),
6    ];
7}

Calling disabled without any parameters disables the action for every row.

Hiding actions

A disabled action still shows up in the menu. When an action makes no sense for a row, hide it instead with the hidden method:

1public function actions(): array
2{
3    return [
4        Action::make('Unpublish', fn (ProductReview $record) => $record->unpublish())
5            ->hidden(fn (ProductReview $record) => ! $record->is_published),
6    ];
7}

Calling hidden without any parameters hides the action for every row. When every action of a row is hidden, the row has no actions menu.

Larasell refuses to run a hidden action, even when someone sends the request by hand.

Hiding a link action, one that uses route or to, only removes it from the menu. The page it links to still opens for anyone who knows the URL. Protect that route yourself, for example with a policy or middleware.