© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
8 replies
Amitoj

HasOne relationship not updating foreignKey in parent model

Hi there, I have two models,
Project
Project
&
ProjectAttachment
ProjectAttachment
. The relation in
Project
Project
model is:
protected $fillable = [
  ...
  'attachment_id',
  ...
]
public function attachment(): HasOne{
    return $this->hasOne(ProjectAttachment::class, foreignKey: 'id', localKey: 'attachment_id');
}
protected $fillable = [
  ...
  'attachment_id',
  ...
]
public function attachment(): HasOne{
    return $this->hasOne(ProjectAttachment::class, foreignKey: 'id', localKey: 'attachment_id');
}

and in
ProjectResource
ProjectResource
class, I am using
Section::make('Attachment')
    ->relationship('attachment')
    ->columns(2)
    ->schema([
        Select::make('type')
            ->options(ProjectAttachmentType::options())
            ->native(false)
            ->reactive()
            ->required(),
        TextInput::make('url')
            ->label('URL')
            ->url()
    ])
Section::make('Attachment')
    ->relationship('attachment')
    ->columns(2)
    ->schema([
        Select::make('type')
            ->options(ProjectAttachmentType::options())
            ->native(false)
            ->reactive()
            ->required(),
        TextInput::make('url')
            ->label('URL')
            ->url()
    ])

Although I was very confident it will work, this is only adding rows in
ProjectAttachment
ProjectAttachment
table and not updating the
attachment_id
attachment_id
in
Project
Project
table when submitting a create record request, resulting into empty columns in
Attachment
Attachment
section above. But if I add
attachment_id
attachment_id
manually in database, it would fill up the columns in
Attachment
Attachment
.

Can someone please help with if I missed anything?
Solution
You are right, Thanks you fixed the issue, I am sorry that the parameters had me confused. In
hasOne
hasOne
relationship,
foreign_key
foreign_key
is actually supposed to be parent's id column in child entity, which in my case would be
project_id
project_id
(or no need to mention as you said). I thought I would only need the
project_id
project_id
column in
belongsTo
belongsTo
relation because it would save me a column. Now I removed
attachment_id
attachment_id
because its no longer in use.
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

hasOne model relationship select list options
FilamentFFilament / ❓┊help
3y ago
Create hasOne in model
FilamentFFilament / ❓┊help
2y ago
Form HasOne relationship
FilamentFFilament / ❓┊help
3y ago
Link to edit hasOne record from parent model
FilamentFFilament / ❓┊help
16mo ago