Relationship error in EditResource

Hi everyone! I'm facing an issue with relations in my EditResource. The relation elements are not all loading properly. For example, DatePicker fields are not filled with relation data, but the title field works fine. Tables structure: table_1: id published (bool) completed (bool) table_2: id table_1_id start (date) end (date) title (string) content (text) Model relation: hasOne from table_1 to table_2 Edit form code:
public function form(Form $form): Form
{
return $form
->schema([
Section::make('Name of the section 1')
->schema([
Toggle::make('published')->required()
]),
Section::make('Name of the section 2')
->schema([
Toggle::make('completed')->required(),
Group::make()
->columns()
->relationship('infos')
->schema([
DatePicker::make('start')
->label('Start date')
->required(),
DatePicker::make('end')
->label('End date')
->required(),
TextInput::make('title')
->required(),
])
]),
Section::make('Name of the section 3')
->relationship('infos')
->schema([
RichEditor::make('content')
]),
]);
}
public function form(Form $form): Form
{
return $form
->schema([
Section::make('Name of the section 1')
->schema([
Toggle::make('published')->required()
]),
Section::make('Name of the section 2')
->schema([
Toggle::make('completed')->required(),
Group::make()
->columns()
->relationship('infos')
->schema([
DatePicker::make('start')
->label('Start date')
->required(),
DatePicker::make('end')
->label('End date')
->required(),
TextInput::make('title')
->required(),
])
]),
Section::make('Name of the section 3')
->relationship('infos')
->schema([
RichEditor::make('content')
]),
]);
}
Issue: Only the title field loads correctly, but start and end DatePicker fields remain empty even though the data exists in the database.
6 Replies
Dennis Koch
Dennis Koch4mo ago
Is start and end casted as a date object? It might be an issue that you use ->relationship('infos') twice
Jérôme
JérômeOP4mo ago
Yes, both start and end are properly casted as datetime:
protected $casts = [
'end' => 'datetime',
'start' => 'datetime'
];
protected $casts = [
'end' => 'datetime',
'start' => 'datetime'
];
But can we use the relationship() method multiple times across different components (like Section, Grid, etc.) that need to access the same related attributes? Or is it limited to a single usage per form?
Dennis Koch
Dennis Koch4mo ago
I'm not sure. Did you try only using one?
Jérôme
JérômeOP4mo ago
Yes, I tried using only one. I also tested using the dot notation (relation.column), but the issue remains. Interestingly, it works when I change the order of the Sections in the form. However, this creates another problem, as I need to keep a specific layout and order for the form fields.
Dennis Koch
Dennis Koch4mo ago
That really sounds like one is overwriting the other one
Jérôme
JérômeOP4mo ago
yes i think, so how it work ? In the doc it's mentionned : https://filamentphp.com/docs/3.x/forms/advanced#saving-data-to-relationships "This functionality is not just limited to fieldsets - you can use it with any layout component. For example, you could use a Group component which has no styling associated with it:" But in the same form, we cannot use multiple relationship() ? I need to alternate between fields from the related model (via relationship()) and fields from the primary model multiple times within the same form. I think it might be possible.

Did you find this page helpful?