What is the best way to save data into multiple tables from one form?
Firstly, I did a search within the other help topics and struggled to find anything so apologies if I missed an example!
For context, I have 2 tables within my database,
projects
and project_addresses
.
When creating a new project, I am using a wizard to capture 3 lots of information project details
, admin details
and address details
the later will be stored in the project_addresses
table and separate from the project details
and admin details
that are stored within the projects
table.
Also I apologise if this is in the documentation, the only thing I found which seemed somewhat related was https://filamentphp.com/docs/3.x/forms/advanced#saving-data-to-relationships but this moves on to talking about loading data which threw me off.24 Replies
you need
->relation('addesses')
on the group of fields that belongs to the project_addresses
tableI don't completely follow, here is my wizard step:
I've tried adding the relation to a couple of different elements but it only exists on a few, such as select elements for example.
which of these components belongs to the
project_addresses
table?This whole step belongs to
project_address
I have 2 other steps that belong to projects
table.
Hmm thinking about it, I don't know if this is the best flow for what I want or need at the moment as I need the project.id
which doesn't actually exist at this point to be added to the project_addresses
row too.
Sorry toeknee, I don't know what I am supposed to be looking at here or whether I phrased my question correctly as that doesn't seem relevant to what I am asking.
Apologies if I misunderstood.You haven't sorry, I pasted this by mistake it was for another chat
haha not to worry!
So on the model if you have a relationship 'address' you should be able to use something like:
Ah thank you toeknee! I did start looking at this I think but I think I stopped because I am unsure if it supports one to many? (one project, many address) but please correct me if I am wrong!
In the meantime I will look at the code you shared 🙂
One to many would need a repeater instead of group 🙂 so you can add multiple addresses for that one instance
Checking out the repeater now, thank you!
Definitely on the right path! Just got to figure out how I can get my enum values for the type select.
->options() ?
Ah hit an error!
There is no column in the
projects
table that references an address. Instead, in the project_addresses
table, there is a column for project_id
Is this back to front?
yeah, got there, was being daft!Can I see your code?
sure, is there anything specific that would help further? (rather than me blindly sharing stuff that is of no help)
Not particularly I just need to see how you have the schema build as it shouldn't be looking for Project Addresses ? it should be a structure column as I assume you don't have the column name as Project Addresses anywhere
Okay, let me grab some code, 2 minutes.
Here is my form, I've removed the first 2 schemas as it is too big for the character count for discord otherwise.
My DB structures are like so:
project:
and project addresses:
Where is your ->relationship() on the repeater 😉
oh dear haha
let me return in just one moment
I've got to get my head down in some work now, but the above link and info should get you going!
all done and fixed!
thanks for your help!
Welcome bud!