I’m using Laravel 9 with php 8.1 in a pretty advanced project. In this project I have a parent form which allows a purchase order document to be created. The po document has at least one child row which is added by clicking an add button using livewire. However when this is saved the document it belongs to has not been created and thus no id i can use. So how would you guys handle such a form?
I’m considering a temporary id which I think is my best option. I had considered using the user id here but if I as the user have 2 such forms on the go then I wouldn’t know which are the correct rows to amend with the document id, worse if I’ve added rows and get logged out these rows will be unattached.
Another option would be to create the document but as it stands it would fail validation due to there being no associated rows.
I’m struggling to find a strategy to deal with this scenario. In a previous project I used an is_draft flag for a similar scenario but it left orphaned items if the process was not fully completed.
thanks
2
Answers
I initially came across a composer package called cjmellor/approval (https://github.com/cjmellor/approval) but quickly realised it was far more than I needed but it did inspire me to into thinking of storing the raw data to use later.
After some thought I decided to create an stdClass to hold the required data for each row in the array and add/update this while creating the PO. I 'saved' this into my cache (I use redis in this app) with a 2 hour expiry. I then updated this cached array in livewire until the form was saved at which point I get the cached array data and updated the po data table with the cached data.
You could refactor the view so Livewire controls both the purchase order form and the child row(s). Then Livewire would be able to keep the rows in their unsaved state until you save the parent form, at which point you’d also associate the rows and save those.
If the data is tightly coupled and interdependent then I would go for that approach.