Use `relationship()` when creating a new record
Hi all,
I have two tables,
consumers
and addresses
. The consumer has a address_id
column:
And addresses
table looks like this:
When I use relationship
method to create Address for Consumer:
then the Address is not created. In addition, when I use the handleRecordCreation
method, I don't see any address-related data in there. Only data for Customer.
Does the relationship()
method work for creating new records? Where could be the problem?
Thank you in advance.9 Replies
Without 'relationship' in the
Address
section, I'm able to read all address-related values in the handleRecordCreation
method.
In the worst-case scenario, I can save the relation manually.
PS: The name of the relation matches the name of the method in the Customer
model:
I think you have your relationship wrong on the Customer model. If the foreign key is on the customers table as address_id, the Customer BelongsTo Address, and Address HasOne Customer.
So try changing the relationship on the Customer model to ...
I'm actually kinda surprised that didn't error out, as with that relation as a HasOne, it should have tried to save the address record with a customer_id, which doesn't exist.
Of course, you are right... However even with that change, I am getting following error:
Does
relationship
work with uuid
s?I'm fairly certain, yes. But I don't use uuids, so don't have firsthand experience. I assume you defined the PK as uuid primary in the migration.
id
of both Customer and Address are created the same way:
Is it ok for you to use a select + createOptionForm ? https://filamentphp.com/docs/3.x/forms/fields/select#creating-a-new-option-in-a-modal
Else, can you try to add a
withDefault
on your relation ? Else in your CreatePage
you can create relation before record created
Other thing, do you have $fillable
/ $guarded
attribute in your both models ?@Ashk thank you for suggestions. Both models have
I know there are multiple ways to create the relation, but I'd prefer something 'built-in' . That's why I am asking.
So try a select or a
withDefault
on your relation address
Select with
createOptionForm
works fine.
But again, that's not ideal. For admin users, who know what are they doing, this may be fine.
But I cannot expect the end-user to know, that thay have to click on the plus icon and create a new record in modal window.
Thank you nevertheless