F
Filament12mo ago
Augus

Relationship not saved to Model on Section

In my UserResource i got a Section as followed:
Section::make('Contactgegevens')
->relationship('contact')
->schema([...
Section::make('Contactgegevens')
->relationship('contact')
->schema([...
This does create a new contact when i fill in the needed fields, but it does not attach that contact to my User. User Model:
public function contact(): HasOne
{
return $this->hasOne(Contact::class, 'id', 'contact_id');
}
public function contact(): HasOne
{
return $this->hasOne(Contact::class, 'id', 'contact_id');
}
Contact Model:
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'contact_id', 'id');
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'contact_id', 'id');
}
My users table has an
contact_id
contact_id
field which should be filled with the created contact. But it isn't. I think the issue is somewhere in my code but for the love of me im not able to see it.
6 Replies
DrByte
DrByte12mo ago
I haven't used that feature yet myself. Is there an example of using a Relation on a Section (or fieldset or grid) in the Filament Demo repo? Perhaps that may help identify anything missing in your code.
Augus
AugusOP12mo ago
@DrByte thanks for your response, i am trying to do whats described in the documentation but it doesn't work. And it isn't used in the demo right now. So apparantly Fillament doesn't see this as a valid relation and i needed to move the contact_id to an user_id in the contacts table. Weird It is the "normal" Laravel way but chancing the expected keys as in my example is a Laravel function Not sure why Filament has issues with it.
awcodes
awcodes12mo ago
Seems to me like your relationship is backwards. To me, a user has one contact and a contact belongs to a user. In which case having contact_id on the user table would make sense. Also why it works when you switch it as you said.
Augus
AugusOP12mo ago
@awcodes thanks for responding, but if i read the docs on https://laravel.com/docs/10.x/eloquent-relationships#one-to-one it's the opposite. In their example A User
hasOne
hasOne
phone and a phone
belongsTo
belongsTo
a user. And without defining a 'foreign_key' Laravel would assume you got an user_id column in the Phone record. That examples works as i changed it now. But in Laravel if for whatever reason you would want a 'contact_id' column on the User record you can define that via the 'foreign_key' and where needed the 'local_key'. Which i initially used to do and should be fine. Except that Filament on that point won't attach the Phone (in my case Contact) to my User. With the sidenote that excistsing records are shown on the edit page. Let me try to create a minimal Reproduction repo and see if there it has the same issue.
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Augus
AugusOP12mo ago
I created an Github issue for this: https://github.com/filamentphp/filament/issues/10253
GitHub
New relation not saved on Section ->relationship() · Issue #10253 ·...
Package filament/filament Package Version V3.1.18 Laravel Version v10.35.0 Livewire Version No response PHP Version PHP 8.2.13 Problem description I got a relation in my User model to my Contact mo...
Want results from more Discord servers?
Add your server