F
Filament11mo ago
.jw.

Accessing pivot model relationships in relationship manager

I have a relationship manager that handles a belongsToMany that is defined with using(SomePivotModel::class). The pivot model has a couple of belongsTo relationships defined. The belongsToMany relationship allows duplicates. If I don't have allowDuplicates() set on the relationship manager table, then I can pull in data from the pivot model relationships in my columns (using a format like pivot.somerelationship.name). If I set the table to allowDuplicates(), I can no longer access those pivot relationships. I can access the other columns in the pivot table, though they are no longer available as pivot.somefield, they are just at the top level. I see in the Filament code that this is because of a longstanding bug in Laravel about how it handles IDs in eloquent, though I'm not completely following the fix. I'm wondering if anyone else has run into this or has an idea of how I can access those pivot relationships when allowDuplicates() is set.
1 Reply
.jw.
.jw.OP11mo ago
To give what seems like a hacky answer to my own question, I am just manually getting the relationship data I need with something like this on the relationship manager table:
->modifyQueryUsing(function (Builder $query): void {
$query->join('foo', 'foo.id', '=', 'pivot_table.foo_id')
->select('relationshipmodel.*','pivot_table.*','foo.thedataineed');
});
->modifyQueryUsing(function (Builder $query): void {
$query->join('foo', 'foo.id', '=', 'pivot_table.foo_id')
->select('relationshipmodel.*','pivot_table.*','foo.thedataineed');
});
Want results from more Discord servers?
Add your server