Skip to content

[Table] - There is no way to access individual "-many" relationship models in column #19121

@khangln-amv

Description

@khangln-amv

Package

filament/tables

Package Version

v4, v5

Laravel Version

v11, v12

Livewire Version

v3, v4

PHP Version

PHP 8.3+

Problem description

Currently, there is a number of columns that support displaying a list of 1-n and n-n relationship: TextColumn::make('tags.name'), ImageColumn::make('colleagues.avatar')->circular()->stacked(), however, there is no "official" way to access other fields or the model itself.

Consider:

// suppose we have a TagModel with columns (name, color)
TextColumn::make('tags.name')
    ->badge()
    ->color( // 'tags.color') //here, $record will be the main table $record, $state will be `tag.name`, $rowLoop will be the row loop of the row itself, not the loop of tags

or

ImageColumn::make('colleagues.avatar')
    ->circular()
    ->stacked()
    ->tooltip(// 'colleagues.name')

Currently, some cases there are workaround, for example:

TextColumn::make('tags.name')
    ->badge()
    ->color(function ($record, $state){
        $tag = $record->tags->firstWhere('name', $state);

        return Color::hex($tag->color);
    })

but at the end of the day, it is still a workaround, it wouldnt work if there is a record with null name, or duplicate name (it doesnt make sense with tag name, but other models may)

Speaking of which, there is currently no workaround for the avatar example, since $user->avatar may be null, or 2 user can use the same img url.

Expected behavior

There should be a way to access the model itself, either directly, or through a $rowLoop-type variable.

function($relationshipRecord) {

}

or

function($model, $relationshipRowLoop) {
    $model->relationship[$relationshipRowLoop->index]
}

Steps to reproduce

  1. Make a new filament project
  2. Make a table
  3. Make a HasMany or BelongsToMany relation
  4. Try to access and customize individual relation model

Reproduction repository (issue will be closed if this is not valid)

https://github.com/filamentphp/demo

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions