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

cxbdia's avatar

reset field + update data via listener?

public function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('question'),
            ])
            ->actions([
                Action::make('Reply')
                    ->form([
                        TextInput::make('replyMessage')
                            ->required()
                            ->live()
                    ])
                    ->modalContent(function (ChatSession $record, Action $action) {
                        return view(
                            'filament.chat-messages.chats',
                            ['chatSession' => $record],
                        );
                    })

                    ->extraModalFooterActions(fn(Action $action): array => [
                        $action->makeModalSubmitAction('Reply', arguments: ['halt' => true]),
                    ])
                    ->action(function ($action, ChatSession $record, array $data, $arguments, Set $set, Get $get) {
                        $chatMessageService = new ChatMessageService();
                        $chatMessageService->create($record->id,$data['replyMessage'],auth()->user()->id,true);
                        
                        if ($arguments['halt'] === true) {
                            $action->halt();
                        }
                    })
                    ->slideOver()
                    ->modalSubmitAction(false)
            ]);
    }
  1. How can I reset the value of replyMessage field? $set , $this->form->fill(); $this->reset('replyMessage') - all 3 are not working when written above halt() and anything written after halt doesnt get executed.

  2. This custom view (filament.chat-messages.chats), loads the chat message of a user & admin. I am dispatching an event from livewire from the frontend, how can I listen to that event of a new message so that the messages get updated in this modal?

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you're working with Filament, which is a Laravel package for building admin panels and customizing actions within tables. Here's a solution to your two questions:

  1. Resetting the replyMessage field: To reset the replyMessage field after the action is executed, you can use the $set callback provided by Filament. However, since you mentioned that $set is not working when written above halt(), and anything written after halt() doesn't get executed, you might need to restructure your code to reset the field before halting the action. Here's an example:
->action(function ($action, ChatSession $record, array $data, $arguments, Set $set, Get $get) {
    $chatMessageService = new ChatMessageService();
    $chatMessageService->create($record->id, $data['replyMessage'], auth()->user()->id, true);
    
    // Reset the replyMessage field before halting the action
    $set('replyMessage', '');

    if ($arguments['halt'] === true) {
        $action->halt();
    }
})
  1. Listening to a Livewire event in the modal: To listen to a Livewire event and update the messages in the modal, you can use Livewire's event listeners. In your Livewire component that represents the modal content, you would add an event listener and define a method to handle the event. Here's an example:

In your Livewire component (assuming it's called ChatMessages):

class ChatMessages extends Component
{
    protected $listeners = ['messageReceived' => 'updateMessages'];

    public function updateMessages($message)
    {
        // Logic to update the messages in the modal
        // This could involve refreshing a property that holds the messages
        // or querying the database again to get the latest messages
    }

    // ... rest of the component
}

In your frontend where you dispatch the event, you would do something like this:

Livewire.emit('messageReceived', message);

Make sure to replace message with the actual data you want to pass to the event handler.

Remember to refresh the component or the specific part of the component that displays the messages to reflect the changes. You can do this by calling $this->emitSelf('refreshComponent') from within your Livewire component or by updating a public property that your component's view is reacting to.

cxbdia's avatar

when I use $set('replyMessage', ''); I get this error message

Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization

Please or to participate in this conversation.