In the context of a Livewire component, such as the one you're using, the mutateFormDataBeforeCreate method does not have direct access to the request object like you would in a typical controller. This is because Livewire components are not tied to the HTTP request lifecycle in the same way.
However, you can capture the request parameter in the mount method and store it as a property of the component. Then, you can use this property within the mutateFormDataBeforeCreate method. Here's how you can do it:
class CreateCommunication extends CreateRecord
{
public $pid;
public function mount(): void
{
$this->pid = request()->get('pid');
}
protected function mutateFormDataBeforeCreate(array $data): array
{
// Now you can access $this->pid here
$data['json_field'] = json_encode([
'pid' => $this->pid,
// Add other parameters as needed
]);
return $data;
}
}
In this solution:
- We define a public property
$pidin the component. - In the
mountmethod, we capture thepidparameter from the request and assign it to the$pidproperty. - In the
mutateFormDataBeforeCreatemethod, we access$this->pidto include it in the data array before saving.
This approach allows you to handle request parameters within a Livewire component effectively.