DEV Community

Süleyman Özgür Özarpacı
Süleyman Özgür Özarpacı

Posted on

1

How to Trigger Another Action in FilamentPHP

If you have an Action in FilamentPHP and want to trigger another action sequentially within it, you can use the Action Chaining feature.

Example Scenario

Suppose you have an action called DuplicateAction, and after it completes, you want the EditAction to open automatically. Below is an example implementation of both actions.

EditAction Example

Here’s how you can define the EditAction:

Action::make('edit')
    ->fillForm(function (array $arguments) {
        $id = $arguments['id'];
        $item = Item::find($id);

        return $item ? $item->toArray() : [];
    })
    ->form(fn () => [
        // Your form fields go here
    ])
    ->action(function (array $arguments, $data) {
        $id = $arguments['id'];

        $item = Item::find($id);
        if (!$item) {
            return;
        }

        $item->update($data);
    });
Enter fullscreen mode Exit fullscreen mode

DuplicateAction Example

Here’s how you can define the DuplicateAction and trigger the EditAction after duplicating:

return Action::make('duplicate')
    ->requiresConfirmation()
    ->modalDescription('Are you sure you want to duplicate this item?')
    ->action(function (array $arguments) {
        $id = $arguments['id'];
        $isEdit = isset($arguments['edit']);

        $item = Item::find($id);
        if (!$item) {
            return;
        }

        $newItem = $item->replicate();
        $newItem->name = $newItem->name . ' (copy)';
        $newItem->afterNode($item)->save();

        if ($isEdit) {
            $this->replaceMountedAction('edit', [
                'id' => $newItem->id,
            ]);
        }
    })
    ->extraModalFooterActions(fn (Action $action): array => [
        $action->makeModalSubmitAction('duplicateAndEdit', arguments: ['edit' => true])
            ->label('Duplicate and Edit'),
    ]);
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. EditAction: Opens a form pre-filled with the data of the selected item. When the user submits the form, it updates the item in the database.
  2. DuplicateAction:
    • Duplicates the selected item and appends " (copy)" to its name.
    • Offers an additional button in the modal footer, labeled "Duplicate and Edit," which duplicates the item and directly triggers the EditAction with the duplicated item's data.

By using this method, you can seamlessly open actions sequentially, enhancing the user experience.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay