phplaravellaravel-filament

How can i automatically insert the current user_id after submitting a form in Filament v3.0


I'm currently working on a project in FilamnentPHP v3 and I am trying to figure out how to automatically pass the current user_id to my database-entity.

I thought of creating a hidden input-field and setting the default value of the field to the current user_id. But it seems like Filament ignores the hidden() TextInput and inserts null or does simply nothing.

My wrong solution

TextInput::make('user_id')
        ->label(__('messages.created_by'))
        ->required()
        ->default(
              Filament::auth()->id()
        )->readOnly()
            ->hidden()

So is there any way to pass values automatically to the database after the form-submit in Filament v3.

I also tried to look through the Filament-docs but I had, no real luck finding something that helps me to solve my problem.


Solution

  • I have found out that it is possible to edit the create Page of the specific resource and add:

    PATH: ./app/Filament/Resources/MyResource/Pages/CreateMyResource.php

        protected function mutateFormDataBeforeCreate(array $data): array
        {
            $data['user_id'] = auth()->id();
    
            return $data;
        }