Insert Data in Pivot Table
I have 2 tables companies and users with many to many relationship, now I want to add data in pivot table when I'm creating new user how can I do that?Please help
Solution:Jump to solution
```php
protected function afterCreate(): void
{
//companies that you want to attach
$companies = [1,2,3..];...
10 Replies
?
What if I don't have the select form when creating a new user. I customize the filament breezy registration and added a company field that's why the user logged in belongs to a company already,.
you can use a hook as afterCreate to sync companies I think
https://filamentphp.com/docs/2.x/admin/resources/creating-records#lifecycle-hooks
Filament
Creating records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
can you give me an example on how to do this sorry I'm new. how can I access the id of the created user so I can attach or sync it with the related table
are you using admin panel, resource? Where are you creating the user?
Yes I'm creating it in the UserResource
you have a Pages/CreateUser.php class right?
yes
Solution
thanks bro