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

Pixelairport's avatar

Cant get request parameter in mutateFormDataBeforeCreate

The code shows my problem. I try to get a parameter from the url before saving. I know I can do it in the model, but I want to do it here, because there are different parameters that I merge into a json field. Is there a way to do it this way?

class CreateCommunication extends CreateRecord
{
    
    // ...

    public function mount(): void
    {
        // Here I get request()->get('pid')
    }

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        // Here I dont get request()->get('pid')
    }
}
0 likes
3 replies
LaryAI's avatar
Level 58

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 $pid in the component.
  • In the mount method, we capture the pid parameter from the request and assign it to the $pid property.
  • In the mutateFormDataBeforeCreate method, we access $this->pid to include it in the data array before saving.

This approach allows you to handle request parameters within a Livewire component effectively.

Pixelairport's avatar

I thought my problem was solved, but I have another problem with this. I need to get $this->pid in my CommunicationResource. I tried so many ways today, but nothing works. I want to do something like this:

class CommunicationResource extends Resource
{
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('name')
                    ->helperText('We use project with ID:' . $this->pid),

I tried to make it simple here. I have a lot more values I need to have default values etc. Or would it be better to do all in the resource? Or better in the model?

Some more info what I want to make:

I want a form with fields, that are not stored in database. There should be only one json field, where I store everything. Some fields are not shown in the form like (default image). The user can upload another image for example. This is all for a mail extension I build, where my team can click parameters which will be send to transaction api. There are also fileds like "show latetest posts", "send voucher",...

Pixelairport's avatar

Im still try to find the best way. Maybe I now found it, after a lot of testing things. I will give the parameter to the resource with mount method in class CreateCommunication extends CreateRecord. But only when I really have a field in my resource, i can also read it in the function protected function mutateFormDataBeforeCreate(array $data).

That is why I add Hidden::make('extended.project_id') to my resource form method. But is that secure? I dont want that anyone can change the code for example with code inspector and change the id. I tested it and even when i change it I get the correct id instead of the new i have changed to. But are there other ways to manipulate the hidden field?

Please or to participate in this conversation.