F
Filament6mo 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()
]);
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');
}
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...
Jump to solution
3 Replies
Reda
RedaOP6mo ago
error
No description
toeknee
toeknee6mo ago
Itm is saying human_resources table doesn't have 'project_manager' column....
Solution
Reda
Reda6mo ago
@toeknee i found the error in the relationship. instead of using "hasOne" i use "belongsTo" and it works
Want results from more Discord servers?
Add your server