how to show a MorphOne table in filament Table?

I am struggling to access MorphOne relationship data in my table. I have Member model with MorphOne relationship with User and Company model. And I am trying display logo. But I am failing everytime. I use below code.
ImageColumn::make(function (Member $record) {
return $record->member_type === 'company' ? $record->memberable->profile->logo : $record->memberable->profile_photo_path;
})->size(200),
ImageColumn::make(function (Member $record) {
return $record->member_type === 'company' ? $record->memberable->profile->logo : $record->memberable->profile_photo_path;
})->size(200),
I get repeat error saying
Filament\Tables\Columns\Column::make(): Argument #1 ($name) must be of type string, Closure given, called in /var/www/html/app/Filament/Admin/Resources/MemberResource.php on line 103
Filament\Tables\Columns\Column::make(): Argument #1 ($name) must be of type string, Closure given, called in /var/www/html/app/Filament/Admin/Resources/MemberResource.php on line 103
.
Solution:
you can use ->state()
Jump to solution
11 Replies
Tally
Tally6mo ago
Hi there, the docs show that you need to pass in a string (column in the database) https://filamentphp.com/docs/3.x/tables/columns/image#overview
Pritbor
PritborOP6mo ago
Hi @Tally .. thanks for quick response. Is not my return is string.
Tally
Tally6mo ago
you then could use a ->formatStateUsing() to change the behaviour
Pritbor
PritborOP6mo ago
You mean, i should choose any random 'string' in make() and use ->formatStateUsing() to define my callback.
Tally
Tally6mo ago
yes.... call it profilepicture 🙂
Pritbor
PritborOP6mo ago
But it say
ImageColumn::formatStateUsing does not exist.
ImageColumn::formatStateUsing does not exist.
Actually I have been able to one at a time either ImageColumn::make('memberable->profile_photo_path') or ImageColumn::make('memberable->profile->logo') for user or company but not generically for both depending upon member_type. since I have MorphOne elation.
Solution
Tally
Tally6mo ago
you can use ->state()
Tally
Tally6mo ago
then use your closure
Pritbor
PritborOP6mo ago
thanks alot its working. So I can use ->state not just for image column rather anywhere for any morph relationship. Thanks alot.
Tally
Tally6mo ago
normally if you set up the relation correctly you can use model.relationshipname.image_field
Pritbor
PritborOP6mo ago
Sure @Tally . I was struggling with Morph relationship. Cause Table needs a way to know Model type.
Want results from more Discord servers?
Add your server