F
Filament17mo 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
MaseaOP17mo ago
Here are how my relationships look like for both User and Course models:
Patrick Boivin
Patrick Boivin17mo ago
Can you share the table() method?
Masea
MaseaOP17mo ago
Patrick Boivin
Patrick Boivin17mo ago
Try AttachAction instead if CreateAction
Masea
MaseaOP17mo 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
MaseaOP17mo ago
It gives me this error when i try to attach an user
Masea
MaseaOP17mo ago
Here's my attachaction:
Patrick Boivin
Patrick Boivin17mo ago
Instead of user_id your custom select, use recordId
Masea
MaseaOP17mo ago
Thanks that works!
Masea
MaseaOP17mo ago
One last issue though, i cannot see users' name on the table with this:
Masea
MaseaOP17mo ago
even though my UserCourse class has user relation
Patrick Boivin
Patrick Boivin17mo ago
Hmm, not sure... is fullName a column on the users table?
Masea
MaseaOP17mo 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 Boivin17mo ago
Yeah probably, can you try ->formatStateUsing(fn ($record) => dd($record)) on the column?
Masea
MaseaOP17mo ago
It gets me this... insteresting, it is the 'users' table!
Masea
MaseaOP17mo ago
i don't get it
Masea
MaseaOP17mo ago
Masea
MaseaOP17mo ago
this works
Patrick Boivin
Patrick Boivin17mo ago
So this should work ->formatStateUsing(fn ($record) => $record->fullName) Lol yes, even better
Masea
MaseaOP17mo ago
i just removed the 'user.' part 😄 alright, thank you so much this really helped me a lot!
Patrick Boivin
Patrick Boivin17mo ago
You're welcome 🙌
Want results from more Discord servers?
Add your server