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