Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

juanborras's avatar

FilamentPHP, get notified when form button is pressed

Hey there!

How can I be notified when a button is pressed inside a relation manager? I want to lock a record as soon as the client clicks on the "create" button, but I don't find how...

Thanks!

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve the functionality of being notified when a button is pressed in a FilamentPHP relation manager, you can utilize Livewire events. FilamentPHP is built on top of Livewire, so you can leverage Livewire's event system to handle button clicks.

Here's a step-by-step solution:

  1. Create a Livewire Component Method: First, ensure that your Livewire component has a method that will handle the logic you want to execute when the button is pressed. For example, you might want to lock a record.

    public function lockRecord()
    {
        // Your logic to lock the record
        // For example, update a database field to mark the record as locked
    }
    
  2. Emit an Event from the Button: In your Filament form, you can emit an event when the button is pressed. You can do this by adding an x-on:click directive to the button, which will emit a Livewire event.

    <button type="button" x-on:click="$wire.emit('lockRecord')">
        Create
    </button>
    
  3. Listen for the Event in the Livewire Component: In your Livewire component, listen for the event and call the method you created.

    protected $listeners = ['lockRecord' => 'lockRecord'];
    
  4. Integrate with Filament: If you're using a Filament form, you might need to customize the form's actions to include this event emission. You can extend or customize the form actions to include the x-on:click directive.

    Here's an example of how you might customize a form action in Filament:

    use Filament\Forms\Components\Actions\Action;
    
    public function form(Form $form)
    {
        return $form
            ->schema([
                // Your form fields here
            ])
            ->actions([
                Action::make('create')
                    ->label('Create')
                    ->action('lockRecord')
                    ->button()
                    ->extraAttributes(['x-on:click' => '$wire.emit("lockRecord")']),
            ]);
    }
    

By following these steps, you can effectively be notified and execute logic when a button is pressed in a FilamentPHP relation manager. This approach leverages Livewire's event system to handle the interaction.

1 like

Please or to participate in this conversation.