F
Filament9mo ago
hxn

conditional table column

Hello i'm trying to make a conditional column.. I've follow this online tutorial to make a settings page https://blog.moonguard.dev/setting-page-with-filament I've made some change to accept media (I'm using curator plugin) and what i want to achieve is a conditional column If the type of my setting is media i want a CuratorColumn. For everything else a TextColumn but i don't know how to make this
return $table
->columns([
Tables\Columns\TextColumn::make('label')
->sortable()
->searchable(),
// If the type is "media" make a CuratorColumn on value
CuratorColumn::make('value')
->circular(),
// Else make a TextColumn on value
Tables\Columns\TextColumn::make('value')
->formatStateUsing(function ($state) {
return match ($state) {
null => 'Empty',
"0" => 'False',
"1" => 'True',
default => 'OK'
};
})
->sortable()
->searchable(),
])
return $table
->columns([
Tables\Columns\TextColumn::make('label')
->sortable()
->searchable(),
// If the type is "media" make a CuratorColumn on value
CuratorColumn::make('value')
->circular(),
// Else make a TextColumn on value
Tables\Columns\TextColumn::make('value')
->formatStateUsing(function ($state) {
return match ($state) {
null => 'Empty',
"0" => 'False',
"1" => 'True',
default => 'OK'
};
})
->sortable()
->searchable(),
])
Create a Simple Settings Page with Filament | MoonGuard - Web Monit...
Learn how to create custom settings page with Filament, from generating migrations to dynamically building the edit form.
Solution:
I've finally opted for the custom view because I don't necessarily need a curatorColumn SettingsResource.php ```php return $table...
Jump to solution
8 Replies
hxn
hxn9mo ago
Or maybe it's not possible?
cheesegrits
cheesegrits9mo ago
Not possible.
DrByte
DrByte9mo ago
A workaround might be to create a custom Curator column, and have it display its media if relevant, else display your custom TextColumn output. Basically your custom column would be a hybrid of both the other columns. Might not make sense for sortable/searchable on it though.
hxn
hxn9mo ago
Thanks for this workaround i will dig into it
Kenneth Sese
Kenneth Sese9mo ago
I’m doing this exact thing with a custom curator column where I display the curator image if it’s available and if not I display the records initials. Both are wrapped in a circle. So I see either the avatar image or the initials depending on what’s available.
Solution
hxn
hxn9mo ago
I've finally opted for the custom view because I don't necessarily need a curatorColumn SettingsResource.php
return $table
->columns([
...
Tables\Columns\ViewColumn::make('value')->view('filament.tables.columns.settingColumn'),
])
...
return $table
->columns([
...
Tables\Columns\ViewColumn::make('value')->view('filament.tables.columns.settingColumn'),
])
...
settingColumn.blade.php
<div class="w-16 flex items-center justify-center">
@php
$type = $getRecord()->type;
$render = null;
switch ($type) {
case 'media':
$media = Awcodes\Curator\Models\Media::where('id', $getState())->first();
$render = $media ? $media->url : false;
break;
default:
$render = $getState();
break;
}
@endphp
@switch($type)
@case("media")
@if ($render)
<img class="w-10 h-10 rounded-full" src="{{ $render }}">
@else
None
@endif
@break
@case("boolean")
{{ $render === "0" ? "False" : "True" }}
@break
@default
{{ $render }}
@endswitch
</div>
<div class="w-16 flex items-center justify-center">
@php
$type = $getRecord()->type;
$render = null;
switch ($type) {
case 'media':
$media = Awcodes\Curator\Models\Media::where('id', $getState())->first();
$render = $media ? $media->url : false;
break;
default:
$render = $getState();
break;
}
@endphp
@switch($type)
@case("media")
@if ($render)
<img class="w-10 h-10 rounded-full" src="{{ $render }}">
@else
None
@endif
@break
@case("boolean")
{{ $render === "0" ? "False" : "True" }}
@break
@default
{{ $render }}
@endswitch
</div>
Joe IW
Joe IW6mo ago
I had a similar use case where I only wanted to display a column for super_admin users, and hide it for ordinary users. I simply stored the columns in an array variable, then conditionally appended to the array if the user was super_admin.
$columns = []; // define columns
if ($user->hasRole('super_admin')) {
array_push($columns, Tables\Columns\TextColumn::make('admin_only_column'));
}

return $table
->columns($columns);
$columns = []; // define columns
if ($user->hasRole('super_admin')) {
array_push($columns, Tables\Columns\TextColumn::make('admin_only_column'));
}

return $table
->columns($columns);
Hope that helps someone.
cheesegrits
cheesegrits5mo ago
Right, but that's a global thing, you are either showing a column or not, depending on some condition. What OP wanted to do was, in the same column, show different column types depending on some condition. So on some rows show a Curator cell, on other rows show a TextColumn cell, within the same column.