F
Filament9mo ago
nn

Filtering options in RelationManager

I have four Resources: GameMatch, Event, Player, Team I have a RelationManager inside the GameMatch resource, for Events. It's successfully working. I have a Select for player_id, when creating a new event on that modal, but it shows all the players of the database. My wish would be to show only the players with a team_id equal to home_team_id or away_team_id from the GameMatch. The code I conjured up is the following:
Select::make('player_id')
->relationship(
name: 'player',
titleAttribute: 'team_id',
modifyQueryUsing: function (Builder $query){
$home_id = GameMatch::query()->pluck('home_team_id');
return $query->where('team_id',$home_id);
}
)
->getOptionLabelFromRecordUsing(function (Player $record) {
$team = Team::find($record->team_id);
return "{$record->name} : {$team->name}";
})
->preload()
->searchable()
->required(),
Select::make('player_id')
->relationship(
name: 'player',
titleAttribute: 'team_id',
modifyQueryUsing: function (Builder $query){
$home_id = GameMatch::query()->pluck('home_team_id');
return $query->where('team_id',$home_id);
}
)
->getOptionLabelFromRecordUsing(function (Player $record) {
$team = Team::find($record->team_id);
return "{$record->name} : {$team->name}";
})
->preload()
->searchable()
->required(),
However I'm receiving a Invalid parameter number, although the query that's displayed on that dialog produces the right results when querying the database. I'm sure I'm doing something wrong but can't figure out what. Thanks for any help.
Solution:
In the end, I figured it out. GameMatch was getting me a collection, and not a contextualized GameMatch. Got there doing it like this:
modifyQueryUsing: function (Builder $query){
return $query->where('team_id',$this->ownerRecord->home_team_id)->orWhere('team_id', $this->ownerRecord->away_team_id);
}
modifyQueryUsing: function (Builder $query){
return $query->where('team_id',$this->ownerRecord->home_team_id)->orWhere('team_id', $this->ownerRecord->away_team_id);
}
...
Jump to solution
1 Reply
Solution
nn
nn9mo ago
In the end, I figured it out. GameMatch was getting me a collection, and not a contextualized GameMatch. Got there doing it like this:
modifyQueryUsing: function (Builder $query){
return $query->where('team_id',$this->ownerRecord->home_team_id)->orWhere('team_id', $this->ownerRecord->away_team_id);
}
modifyQueryUsing: function (Builder $query){
return $query->where('team_id',$this->ownerRecord->home_team_id)->orWhere('team_id', $this->ownerRecord->away_team_id);
}