Struggling filtering a table

Lets say i have this filament table:
return $table
->query(
    Song::query()
        ->whereHas('plays', function ($q) {
            $q->where('is_listened', true);
        })
        ->withCount('plays')
        ->orderByDesc('plays_count')
)


the objective is to show the songs with more plays.

eloquent relationships explanation:
tables:
songs: id, name
artists: id, name

artist_song: artist_id, song_id

user_song_play_history: user_id, song_id, is_played

relationships:
Song model:
public function artists(): BelongsToMany
{
    return $this->belongsToMany(Artist::class)->withTimestamps();
}

public function plays(): BelongsToMany
{
    return $this->belongsToMany(User::class, UserSongPlayHistory::class);
}


Artist model:
public function songs(): BelongsToMany
{
    return $this->belongsToMany(Song::class)->withTimestamps();
}


ArtistSong pivot model:
public function artist(): BelongsTo
{
    return $this->belongsTo(Artist::class);
}

public function song(): BelongsTo
{
    return $this->belongsTo(Song::class);
}


UserSongPlayHistory pivot model:
public function song(): BelongsTo
{
    return $this->belongsTo(Song::class);
}

public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}


at the moment works just fine. the problem is when i try to add a filter to filter by user. i get and so far havent found how to solve it:
Expected type 'Illuminate\Database\Eloquent\Builder'. Found 'Closure(mixed, mixed): void'

->filters([
    Tables\Filters\SelectFilter::make('user')
        ->relationship('plays.user', 'name')
        ->default([auth()->id()])
        ->apply(function ($query, $value) {
            $query->whereHas('plays', function ($q) use ($value) {
                $q->where('is_listened', true)
                    ->where('user_id', $value);
            });
        })
    ,
])
Was this page helpful?