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
awcodes5mo ago
Just use a Model observer.
Solution
awcodes
awcodes5mo ago
That would be the easiest thing to do.
fredricretzko
fredricretzko5mo ago
OK, so after the School model is created, the observer would then attach the Teacher. I think that could work.
awcodes
awcodes5mo ago
Basically, yea. Assume the teacher is the logged in user it should be pretty straightforward.
fredricretzko
fredricretzko5mo ago
Very good. Thanks for the quick response!
awcodes
awcodes5mo 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
Np5mo ago
How can we use observer for Belongs to many relationship pivot table.