FilamentF
Filament2y ago
Reda

Handle nullable relationship in edit form

I have "Project" and "HumanResource" models.
Each project has a "project manager" and "operational_manager", both belong to "HumanResource" model.

i can create a project without mentionning the project and operational managers.
In this case the "project_manager" and "operational_manager" will be null.

When i don't set those fields i get the error in attachment.
Otherwise, i pass normally.

EditRecord Code :
Section::make()->schema([
                        Forms\Components\Select::make('project_manager')
                            ->relationship(name: 'projectManager', titleAttribute: 'full_name') 
                            ->searchable()
                            ->preload()
                            ->nullable(),
                        Forms\Components\Select::make('operational_manager')
                            ->relationship(name: 'operationalManager', titleAttribute: 'full_name')
                            ->searchable()
                            ->preload()
                            ->nullable()
]);


relationship code in Project model :
public function projectManager()
    {
        return $this->hasOne(HumanResource::class, 'project_manager');
    }

    public function operationalManager()
    {
        return $this->hasOne(HumanResource::class, 'operational_manager');
    }
Solution
@toeknee i found the error in the relationship.
instead of using "hasOne" i use "belongsTo" and it works
Was this page helpful?