Form save relationship with other belongs to (user_id.etc) how?

Let's save I have a Filament form that is supposed to create a Book record. The books table has user_id Then in it I have a repeater field that allow me to create BookChapter records On my book_chapters tables I have book_id and user_id In my submit() code I have something like this
$book = auth()->user()->create($this->form->getState());
$this->form->model($book)->saveRelationships(); // chapters, book media.etc
$book = auth()->user()->create($this->form->getState());
$this->form->model($book)->saveRelationships(); // chapters, book media.etc
It is in the second line I have issue, because user_id is not passed into my relationship models (book chapters, media.etc). Only book_id is filled How do i do so in a clean way? I thought about adding a hidden user_id field to my repeaters / spatie media uploads.etc but there is a risk of user manipulating the browser DOM before submission and what if what I need to do is not user_id? Thank you
5 Replies
Dan Harrin
Dan Harrin2y ago
there is a mutateRelationshipDataBeforeCreateUsing() on the repeater I think which you can use to set book_id
pocket.racer
pocket.racerOP2y ago
saveRelationships() will set the book_id so i assume this is for user_id and other foreign ids? is this an admin only method or filament forms can use it too?
Dan Harrin
Dan Harrin2y ago
->mutateRelationshipDataBeforeCreateUsing() can be chained onto the Repeater
pocket.racer
pocket.racerOP2y ago
what's the difference between this and mutateRelationshipDataBeforeFillUsing and mutateRelationshipDataBeforeSaveUsing?
toeknee
toeknee2y ago
One modifies it before filling the form and one modifies it before saving the form.
Want results from more Discord servers?
Add your server