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:
-
Ensure that the
generateIdentifiermethod in yourChatSessionmodel is static and returns a string that can be used as the identifier. -
Modify your
newChatActionmethod to generate the identifier and pass it to the view usingviewData. -
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.