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 AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay