Any ideas here? Why would Laravel use the team_id instead of the user_id when the WHERE clause clearly says WHERE user_id = ? ? This makes no sense, and it's driving me bonkers.
Wrong model ID in belongsToMany relationship's aggregate query
I am using Filament and building a multi-tenant application. I used laravel-shift/blueprint to scaffold the database and models. I have re-re-reviewed the multi-tenant setup in the Filament docs, and am reasonably certain I did this correctly. In the Team and User models, I have the following relationships.
// Team.php
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
// User.php
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class);
}
The pivot table, team_user is correctly populated with user IDs and team IDs; however, the aggregate query is using the ID of the team (12), not the user (2).
SELECT COUNT(*) AS aggregate
FROM teams
WHERE EXISTS (
SELECT *
FROM users
JOIN team_user on users.id = team_user.user_id
WHERE teams.id = team_user.team_id
AND users.id = 12
)
This seems to be causing the list table on the front end not to populate, despite the query that gets the list of teams using the correct user ID and having the expected results.
SELECT teams.*, team_user.user_id AS pivot_user_id, team_user.team_id AS pivot_team_id
FROM teams
JOIN team_user ON teams.id = team_user.team_id
WHERE team_user.user_id = 6
I'm sure it's something trivial that I am overlooking. Any help would be greatly appreciated!
Please or to participate in this conversation.