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
Tally2mo 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
Pritbor2mo ago
Hi @Tally .. thanks for quick response. Is not my return is string.
Tally
Tally2mo ago
you then could use a ->formatStateUsing() to change the behaviour
Pritbor
Pritbor2mo ago
You mean, i should choose any random 'string' in make() and use ->formatStateUsing() to define my callback.
Tally
Tally2mo ago
yes.... call it profilepicture 🙂
Pritbor
Pritbor2mo 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
Tally2mo ago
you can use ->state()
Tally
Tally2mo ago
then use your closure
Pritbor
Pritbor2mo ago
thanks alot its working. So I can use ->state not just for image column rather anywhere for any morph relationship. Thanks alot.
Tally
Tally2mo ago
normally if you set up the relation correctly you can use model.relationshipname.image_field
Pritbor
Pritbor2mo 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
More Posts
Section Heading-Background-ColorHi - Is it possible to change the background-color of a section-heading? I already tried the `extraAExecute filament commands inside a filament pluginHi, I am making a filament plugin which includes resources, models, relationmanagers, etc... Is theIs there a method to disable select row (checkbox) and display just the table with actions, filtersCurrently i'm hiding the checkbox prefixed with each row with css but is there an offical way to remexport notification message doesn't change base on localeAs we can see from image, I have translation file in `action/km/export` but on the notification listMultiple Modal IssueI stumbled upon an issue where I have multiple actions(opening modals). Whenever I open a modal, cloSQLSTATE[IMSSP]: Field 34 returned no data.Currently, I am facing an issue in displaying data from SQL Server. Why am I encountering an error lQuickly switching routes from panel nav, cause blank page or freezeswitching routes several times and quickly cause: in firefox: - blank page and this console error: "I have the navbar overlapping issue inside a resource with inbuilt database notification openmodalI have the navbar overlapping issue inside a resource with inbuilt database notification openmodalwhen using TrashedFilter Bulk actions disappearWhen using TrashedFilter, Bulk actions disappear, They show up again if TrashedFilter:make('another-HasManyThrough repeater relationI am having the following problem where I want to show results in a repeater from a hasManyThroug re