HasManythrough showing wrong owner record Id
Hey đź‘‹
In my model I have a relationship
public function resultLog()
{
return $this->hasManyThrough(
ResultLog::class, Result::class,
'user_id', 'result_id', 'id', 'id'
);
}
I define primary key in my modal as a user_id, but for some reason the relation manager is showing wrong user_id of the owner record, it shows random user_id.
14 Replies
I'm not 100% sure, but I think this is caused by a problem in Eloquent, and fixed in Filament v3.
I remember having this issue in v2, but then I moved that project over to testing v3, and the problem went away at some point. Looking at the v3 code, it specifically references the Laravel issue, and applies the workaround for it when using HasManyThrough relationships ...
What I suspect is happening is that Eloquent is overwriting one of your 'id' with the other.
I may of course be wrong, and I know this doesn't help your current predicament.
https://github.com/filamentphp/filament/blob/3.x/packages/tables/src/Table.php#L1543
GitHub
filament/Table.php at 3.x · filamentphp/filament
Admin panel, form builder and table builder for Laravel. Built with the TALL stack. Designed for humans. - filament/Table.php at 3.x · filamentphp/filament
you are 100% correct, i have exactly the same problem, it's overwriting 'user_id' with the strange id which doesn't exist in the database. is it safe to upgrade to V3? is it working fine with laravel 9? i have upgrade to laravel 10 but most of the packages are not compatible with laravel 10. thank you so much for the detail explanation.
please don’t upgrade to v3
you can probably override getTableQuery() to apply this fix in v2
Alright, let me test it.
@Dan Harrin it doesn't work, may be i am doing something wrong. could you please share a code that need to be added to my relation getTableQuery()
the code that was sent in that github link
look at what the default getTableQuery() is in v2 and then apply that v3 fix
Sorry i still didn't get it, do i need to find getTableQuery() in filament package and replace it with the above code line 1543?
this is what i have done in my relation getTableQuery()
protected function getTableQuery(): Builder
{
$relationship = $this->getRelationship();
if (! $relationship) {
return null;
}
$query = $relationship->getQuery();
return parent::getTableQuery()->select($query->getModel()->getTable() . '.*');
}
GetTableQuery will be on the ListRecord class under pages. Not on the Resource.
Think of the resource class as a “controller” for the CreateRecord, EditRecord and ListRecord. They share some thing but have they’re own methods that only apply to that context.
@awcodes got it, i move the code to ListRecord but it still doesn't resolve the above issue.
Sorry, I don’t have an answer to your issue, was just answering the question of where it should go.
Alright, thank you @Dan Harrin could comments on this issue,.
This query seems wrong though. If you set up the relationship correct you should be pulling it off the model not checking it with ->relationship()
yes i try that as well.
Yeah, crap, I should have said don't even think about running v3, it's still in early alpha, I'm just using it for funsies, try out new features and getting some of my plugin code prepped for it.
I just happened to be looking at that exact bit of code and the Laravel issue when this question popped up.
@Dan Harrin Checking to see if you could provide me some solution of this issue please.