Subquery in table filter

this is the query:
->query(
    Song::query()
        ->withCount([
            'plays' => function ($q) {
                $q->where('is_listened', true);
                // $q->where('user_song_play_history.user_id', 1);
            },
        ])
        ->orderByDesc('plays_count')
)


im trying to show the song name, and how many times the song is listened. like this, it works. the problem comes when i want to filter by certain user:
Tables\Filters\SelectFilter::make('user')
    ->relationship('plays', 'name')
    ->query(function (Builder $query, array $data): Builder {
        return $query->when(($data['value'] ?? null) > 0, function (Builder $query) use ($data) {
            $query->withCount(['plays' => function ($q) use ($data) {
                $q->where('is_listened', true);
                $q->where('user_song_play_history.user_id', $data['value']);
            }]);
        });
    })

it just doesnt work. the field to be filtered is the
user_song_play_history.user_id
. is this the right way to do it?

if i manually filter it (uncommenting the commented line in the query fn) works, but if i filter via the gui, it doesnt




old question: ❓┊helpStruggling filtering a table
Was this page helpful?