F
Filament6mo ago
Kymset

Relationship manager - Issues when using a union relation

I am building an application for tracking chess games. I want to be able to look at a player and see all games they have played. However I need a union query as I do not know whether a Player was Player 1 or Player 2. I have a model of Game which has the following properties:
'player_1_id',
'player_2_id',
'winner_id',
'player_1_id',
'player_2_id',
'winner_id',
Then I have a Player Model with the relation function of:
public function games()
{
return $this->hasMany(Game::class, 'player_1_id')
->union(Game::where('player_2_id', $this->id));
}
public function games()
{
return $this->hasMany(Game::class, 'player_1_id')
->union(Game::where('player_2_id', $this->id));
}
In tinker I am able to return all games that belong to a user. I have created a relationship manager for the Player Resource but I get an error. By the looks of it Filament is adding an order by games.id. I am not too sure how to proceed further to prevent this error. Can I tell filament not to have an order by?
No description
No description
1 Reply
Kymset
KymsetOP6mo ago
Commenting to keep this active Bump Bumping up I think I have found a solution but not sure if it is a good one. If anyone (especially filament team) could say whether it is the best solution please let me know. Within my relationship manager I have had to override this function:
public function getFilteredSortedTableQuery(): Builder
{
$query = $this->getFilteredTableQuery();

$this->applyGroupingToTableQuery($query);

// $this->applySortingToTableQuery($query);


return $query;
}
public function getFilteredSortedTableQuery(): Builder
{
$query = $this->getFilteredTableQuery();

$this->applyGroupingToTableQuery($query);

// $this->applySortingToTableQuery($query);


return $query;
}
This means that I cannot have sortable on any of my columns. I assume there is a better way which would be to override the applySortingToTableQuery so the order gets applied correctly
Want results from more Discord servers?
Add your server