F
Filament13mo ago
Masea

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...
class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';

protected static ?string $modelLabel = 'Student';

protected static ?string $title = 'Enroll Students';

protected static ?string $recordTitleAttribute = 'id';

public static function form(Form $form): Form
{
return $form
->schema([
Select::make('user_id')
->label('Student')
->required()
->options(function (RelationManager $livewire): array {
// code to get the users
})
->searchable(),
]);
}
class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';

protected static ?string $modelLabel = 'Student';

protected static ?string $title = 'Enroll Students';

protected static ?string $recordTitleAttribute = 'id';

public static function form(Form $form): Form
{
return $form
->schema([
Select::make('user_id')
->label('Student')
->required()
->options(function (RelationManager $livewire): array {
// code to get the users
})
->searchable(),
]);
}
when i try to create a new entry, the realtionshipmanager tries to insert into the users table instead of user_courses table
22 Replies
Masea
Masea13mo ago
Here are how my relationships look like for both User and Course models:
Patrick Boivin
Patrick Boivin13mo ago
Can you share the table() method?
Masea
Masea13mo ago
Patrick Boivin
Patrick Boivin13mo ago
Try AttachAction instead if CreateAction
Masea
Masea13mo ago
Alright. But after i change it to attachaction my select options come empty. Any idea why? This is how it looks like atm:
Masea
Masea13mo ago
It gives me this error when i try to attach an user
Masea
Masea13mo ago
Here's my attachaction:
Patrick Boivin
Patrick Boivin13mo ago
Instead of user_id your custom select, use recordId
Masea
Masea13mo ago
Thanks that works!
Masea
Masea13mo ago
One last issue though, i cannot see users' name on the table with this:
Masea
Masea13mo ago
even though my UserCourse class has user relation
Patrick Boivin
Patrick Boivin13mo ago
Hmm, not sure... is fullName a column on the users table?
Masea
Masea13mo ago
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
Patrick Boivin
Patrick Boivin13mo ago
Yeah probably, can you try ->formatStateUsing(fn ($record) => dd($record)) on the column?
Masea
Masea13mo ago
It gets me this... insteresting, it is the 'users' table!
Masea
Masea13mo ago
i don't get it
Masea
Masea13mo ago
Masea
Masea13mo ago
this works
Patrick Boivin
Patrick Boivin13mo ago
So this should work ->formatStateUsing(fn ($record) => $record->fullName) Lol yes, even better
Masea
Masea13mo ago
i just removed the 'user.' part 😄 alright, thank you so much this really helped me a lot!
Patrick Boivin
Patrick Boivin13mo ago
You're welcome 🙌