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

diazwatson's avatar

Send and retrieve data from ViewField

I have a button in a custom list view, when the button is pressed a modal should open and show a randomly generated identifier to the user.

The user is not expected to do anything or interact with the identifier, only see it.

If the modal form is submitted, I need to use that randomly generated identifier to create a new record in the database.

To show the identifier to the user I decided to use \Filament\Forms\Components\ViewField and I am trying to pass the identifier to the view of the ViewField using ->viewData() but:

  • $arguments is not accessible in the ViewField
  • ->viewData() does not seems to do anything (anything I put in here doesn't show in the ViewField view

What an I doing wrong?

// resources/views/filament/resources/chat/pages/list.blade.php
<x-filament-panels::page>
	<div class="flex flex-wrap justify-evenly h-full flex-col content-center">
        <div class="w-44">
                         {{ ($this->newChatAction)(['chat_id' => $chat->id, 'identifier' => \App\Models\ChatSession::generateIdentifier()]) }}
         </div>
	</div>
</x-filament-panels::page>

// \App\Filament\Play\Resources\ChatResource\Pages\ChatList
   protected static string $view = 'filament.resources.chat.pages.list';

    public function newChatAction()
    {
        return Action::make('new')
            ->label(__('New Chat'))
            ->slideOver()
            ->form([
                ViewField::make('identifier')
                    ->label('Room Identifier')
                    ->view('filament.chat.components.identifier')
                    ->viewData([
                        'identifier' => '123', // this doesn't show anywhere in the view
                    ]),
            ])
            ->modalSubmitActionLabel('Start')
            ->modalSubmitAction(fn (StaticAction $action, $arguments) => $action->url(ChatList::getUrl().DIRECTORY_SEPARATOR.$arguments['chat_id'].DIRECTORY_SEPARATOR.$arguments['identifier']));

    }
0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like you're trying to pass data to a ViewField component in Filament and then use that data when submitting a modal form. Here's a step-by-step solution to achieve this:

  1. Ensure that the generateIdentifier method in your ChatSession model is static and returns a string that can be used as the identifier.

  2. Modify your newChatAction method to generate the identifier and pass it to the view using viewData.

  3. In your view file, make sure you are accessing the passed data correctly.

Here's how you can modify your newChatAction method:

public function newChatAction()
{
    $identifier = \App\Models\ChatSession::generateIdentifier(); // Generate the identifier

    return Action::make('new')
        ->label(__('New Chat'))
        ->slideOver()
        ->form([
            ViewField::make('identifier')
                ->label('Room Identifier')
                ->view('filament.chat.components.identifier')
                ->viewData([
                    'identifier' => $identifier, // Pass the generated identifier to the view
                ]),
        ])
        ->modalSubmitActionLabel('Start')
        ->modalSubmitAction(function (StaticAction $action, $data) use ($identifier) {
            // Use the generated identifier when creating a new record in the database
            // You might need to adjust this part based on how you handle the record creation
            $chatSession = new \App\Models\ChatSession();
            $chatSession->identifier = $identifier;
            $chatSession->save();

            // Redirect or perform additional actions as needed
        });
}

In your view file (resources/views/filament/chat/components/identifier.blade.php), you should access the identifier like this:

<div>
    {{-- Display the identifier to the user --}}
    <p>{{ $identifier }}</p>
</div>

Make sure that the view file exists and is correctly named, and that the identifier variable is being printed within the view.

With these changes, when the button is pressed, the modal should open and show the randomly generated identifier to the user. When the form is submitted, the same identifier will be used to create a new record in the database.

1 like
diazwatson's avatar

Since there won't be any interaction expected from the user, I would probably better use an infolist šŸ¤”

Please or to participate in this conversation.