Multi-tenancy and createOptionForm on Select, Field 'company_id' doesn't have a default value

I have a resource that takes Contacts as a Select options. I want to add a Create form to the Select to create a new Contact but when saving get the following error:

SQLSTATE[HY000]: General error: 1364 Field 'company_id' doesn't have a default value


The
ContactResource
form works when used standalone from the menu, but does not when used as the
createOptionForm


Forms\Components\Select::make('Contact')
  ->label('Send applications to')
  ->relationship('contacts', 'name')
  ->createOptionForm(fn(Form $form) => ContactResource::form($form)),



// the contact resource form

public static function form(Form $form): Form
  {
      return $form
        ->schema([
            Forms\Components\Section::make()
                ->schema([
                    Forms\Components\TextInput::make('name')
                        ->required(),
                    Forms\Components\TextInput::make('email')
                        ->required(),
                    Forms\Components\TextInput::make('telephone')
                        ->required(),
                    Forms\Components\Textarea::make('address')
                        ->required(),
                ])
            ->columns(2),
        ]);
}
Solution
Here’s now to do it for anyone finding this:

createOptionAction()
is where you need to pass in your additional data

Forms\Components\Select::make('Contact')
  ->createOptionForm(fn(Form $form) => ContactResource::form($form))
  ->createOptionAction(fn ($action) => $action->mutateFormDataUsing(function ($data) {
      $data['company_id'] = auth()->user()->current_company_id;
      
      return $data;
  })),
Was this page helpful?