© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
1 reply
BBB

TextColumn BelongsToMany

Hello,

I have a question regarding TextColumn and accessing attributes from a BelongsToMany relationship.

Consider the following case (I've simplified it to get to the point) :

class Drug extends Model
{
    public function routes(): BelongsToMany
    {
        return $this->belongsToMany(Route::class);
    }
}

class Route extends Model
{
    protected function test(): Attribute
    {
        return Attribute::make(get: fn () => 'test');
    }
}
class Drug extends Model
{
    public function routes(): BelongsToMany
    {
        return $this->belongsToMany(Route::class);
    }
}

class Route extends Model
{
    protected function test(): Attribute
    {
        return Attribute::make(get: fn () => 'test');
    }
}

So, I can do this in Filament:
TextColumn::make('routes.id')
TextColumn::make('routes.id')

But not this:
TextColumn::make('routes.test')
TextColumn::make('routes.test')

My solution :
TextColumn::make('routes')->getStateUsing(
    fn (Model $model) => $model
        ->routes
        ->pluck('test')
        ->join(', ')
)
TextColumn::make('routes')->getStateUsing(
    fn (Model $model) => $model
        ->routes
        ->pluck('test')
        ->join(', ')
)

Is there a more elegant solution? Especially since in the above case, I'm forced to eager load the routes, otherwise, I'm in an n+1 situation.

Any help? Thanks in advance!
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

Binding TextColumn with a BelongsToMany relation
FilamentFFilament / ❓┊help
3y ago
BelongsToMany TextColumn first_name last_name searchable
FilamentFFilament / ❓┊help
3y ago
TagsInput belongsToMany
FilamentFFilament / ❓┊help
2y ago
Relationship belongstoMany
FilamentFFilament / ❓┊help
3y ago