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

aldriguz's avatar

Filament 1 to 1 relationship error, layout is not associating FK for child

I have a Client model with user relationship of type BelongsTo, and the User model with client relationship of type HasOne

// Client model
public function user()
{
    return $this->belongsTo(User::class);
}
// User model
public function client()
{
    return $this->hasOne(Client::class);
}

so I created a FieldSet on filament ClientResource like this, it renders fine for an edit/update operation but when creating a new record, the form renders fine, but the relationship is not associating the user_id column (on the code had also the same relationship with Tenant / tenant_id with the same issue)

public static function form(Form $form): Form
{
    $domain = '.' . config('tenancy.central_domains')[0];

    return $form
        ->schema([
            Forms\Components\TextInput::make('name')->label('Nombre')->required(),
            Forms\Components\TextInput::make('company_name')->label('Empresa')->required(),
            Forms\Components\TextInput::make('contact_number')->label('Telefono')->required(),
            Forms\Components\TextInput::make('ruc')->label('RUC')->required(),
            Forms\Components\Select::make('regimen_id')
                ->label('Regimen')
                ->relationship('regimen', 'name'),
            Forms\Components\TextInput::make('sunat_username')->label('Usuario SUNAT')->required(),
            Forms\Components\TextInput::make('sunat_password')->label('Contraseña SUNAT')->password()->required(),
            Forms\Components\Fieldset::make('Usuario')
                ->relationship('user')
                ->schema([
                    Forms\Components\TextInput::make('name')->label('Usuario')->required(),
                    Forms\Components\TextInput::make('email')->label('Email')->email()->required(),
                    Forms\Components\TextInput::make('password')->label('Contraseña')->password()->required(),
                ]),
            Forms\Components\Fieldset::make('Inquilino')
                ->relationship('tenant')
                ->schema([
                    Forms\Components\TextInput::make('id')->label('Dominio')
                        ->prefix('https://')
                        ->suffix($domain)
                        ->required()
                ])
        ]);
}

When doing submit on create this is the error I got. Has anyone know how to fix this issue?

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

insert into `clients` (`name`, `company_name`, `contact_number`, `ruc`, `regimen_id`, `sunat_username`, `sunat_password`, `updated_at`, `created_at`) values (cliente 1, company 1, 1212414124, 1222223333, 2, sdafafsasf, 1231421241, 2023-07-13 04:04:42, 2023-07-13 04:04:42)

Also: I found this repo that shows up the same bug that I have https://github.com/Uolsen/filament-hasone-bug

0 likes
1 reply
tisuchi's avatar

@aldriguz That could be one of the possible solutions in your ClientResource file.

public static function create(Form $form, $record): void
{
    $clientData = $form->get('record');
    $userData = $form->get('user');

    $user = User::create($userData);
    
    $clientData['user_id'] = $user->id;
    $client = Client::create($clientData);
}

public static function update(Form $form, $record): void
{
    $clientData = $form->get('record');
    $userData = $form->get('user');

    $record->update($clientData);
    $record->user->update($userData);
}

1 like

Please or to participate in this conversation.