© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
1 reply
Eric

How to show count of relationship from a belongs to many through relationship?

models:
song
playlist
artist

song n n artist
playlist n n song

a playlist has many artist through their songs. idk how to set that relationship in the playlist model. so im doing it like this:
file: playlistresource - inside table columns:
Tables\Columns\TextColumn::make('artists_count')
    ->getStateUsing(function (Playlist $record): int {
        return $record->songs->map(function (Song $song) {
            return $song->artists()->pluck('artists.id');
        })->unique()->flatten()->count();
    })
    ->sortable()
,
Tables\Columns\TextColumn::make('artists_count')
    ->getStateUsing(function (Playlist $record): int {
        return $record->songs->map(function (Song $song) {
            return $song->artists()->pluck('artists.id');
        })->unique()->flatten()->count();
    })
    ->sortable()
,
is there any potential improvements in the query?

if i knew how to set it, id do it like this. im trying to find out how to do it.
Tables\Columns\TextColumn::make('artists_count')
    ->counts('artists')
    ->sortable()
,
Tables\Columns\TextColumn::make('artists_count')
    ->counts('artists')
    ->sortable()
,
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to show Count relationship in table?
FilamentFFilament / ❓┊help
3y ago
Belongs to many showing error
FilamentFFilament / ❓┊help
2y ago
Filtering a many-to-many relationship.?
FilamentFFilament / ❓┊help
3y ago
many to many relationship
FilamentFFilament / ❓┊help
16mo ago