Attach auth()->user() to BelongsToMany model

Hi - I've been coding in php for 10+ years, three years in Laravel, two in Livewire and just getting into Filament.
I've set up a new PanelBuilder (School) which my users (Teacher) will use to update their record. Teacher and School have a BelongsToMany relationship.
I need help understading how to automatically attach the auth()->id() to the school_teacher model when the Teacher adds a school through the CreateSchool form as it doesn't make sense to have the teacher add themselves via the EditSchool form after they've created the school. Your help in explaining or pointing me to the right documentation is appreciated!
Solution:
That would be the easiest thing to do.
Jump to solution
7 Replies
awcodes
awcodes10mo ago
Just use a Model observer.
Solution
awcodes
awcodes10mo ago
That would be the easiest thing to do.
fredricretzko
fredricretzkoOP10mo ago
OK, so after the School model is created, the observer would then attach the Teacher. I think that could work.
awcodes
awcodes10mo ago
Basically, yea. Assume the teacher is the logged in user it should be pretty straightforward.
fredricretzko
fredricretzkoOP10mo ago
Very good. Thanks for the quick response!
awcodes
awcodes10mo ago
You could also do it with the form event lifecycle but I think an observer would be better here in case you have to reuse the form somewhere else.
Np
Np10mo ago
How can we use observer for Belongs to many relationship pivot table.
Want results from more Discord servers?
Add your server