relationshipmanager for belongsToMany relationship with pivot table tries to insert into wrong table
Hey guys might there be any reason that a relation manager tries to insert into the relation table instead of the pivot table?
I currently have usersrelationmanager that is tied to courseresource. my course and users do have many-to-many relationship with a pivot table. inside usersrelationmanager, i have my relationship set to 'users', but when i try to add a new student for a course, it tries to insert an item to the users table...
when i try to create a new entry, the realtionshipmanager tries to insert into the users table instead of user_courses table
![](https://cdn.answeroverflow.com/1128377024126390362/image.png)
22 Replies
Here are how my relationships look like for both User and Course models:
![](https://cdn.answeroverflow.com/1128377420211302430/image.png)
![](https://cdn.answeroverflow.com/1128377420588781709/image.png)
Can you share the
table()
method?![](https://cdn.answeroverflow.com/1128377710738149416/image.png)
Try
AttachAction
instead if CreateAction
Alright. But after i change it to attachaction my select options come empty. Any idea why? This is how it looks like atm:
![](https://cdn.answeroverflow.com/1128378332791177396/image.png)
Probably this https://filamentphp.com/docs/2.x/admin/resources/relation-managers#attaching-with-pivot-attributes
Filament
Relation managers - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
It gives me this error when i try to attach an user
![](https://cdn.answeroverflow.com/1128379935157923911/image.png)
Here's my attachaction:
![](https://cdn.answeroverflow.com/1128380019626999939/image.png)
Instead of
user_id
your custom select, use recordId
Thanks that works!
One last issue though, i cannot see users' name on the table with this:
![](https://cdn.answeroverflow.com/1128380558435692624/image.png)
even though my UserCourse class has user relation
Hmm, not sure... is
fullName
a column on the users table?it is an attribute for the User class but it doesn't matter because i can't get the 'name' either which is a column. i can get user_id here though
looks like this gets info from the user_courses table
Yeah probably, can you try
->formatStateUsing(fn ($record) => dd($record))
on the column?It gets me this... insteresting, it is the 'users' table!
![](https://cdn.answeroverflow.com/1128381822389203144/image.png)
i don't get it
![](https://cdn.answeroverflow.com/1128381918749143120/image.png)
this works
So this should work
->formatStateUsing(fn ($record) => $record->fullName)
Lol yes, even betteri just removed the 'user.' part 😄
alright, thank you so much this really helped me a lot!
You're welcome 🙌