You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've extended the TextColumn/TextEntry and added a feature to include a link if the data has a resource. Hopefully, this can be added to Filament as a default option, but feel free to use it in the meantime.
`<?php
namespace App\Extended\Tables\Columns;
use Filament\Tables\Columns\TextColumn as ExtendedTextColumn;
use Illuminate\Support\HtmlString;
class TextColumn extends ExtendedTextColumn
{
protected function setUp(): void
{
parent::setUp();
}
// You should remove the clickable row by using recordUrl(null) or recordAction(null).
// If you don't use recordUrl(null), the resource will have an HTML issue.
public function listWithLinks( $resourceClass = null, $page = 'view', $columnText = 'name', $shouldOpenInNewTab = true )
{
$newTab = !$shouldOpenInNewTab ?: ' target="_blank"';
return $this->formatStateUsing( function ( $state, $record ) use ( $resourceClass, $page, $columnText, $newTab ) {
$relationName = explode( '.', $this->name )[ 0 ];
$getCollection = $record->$relationName;
if ( $this->isBadge() || $this->isListWithLineBreaks() ) {
$getModel = $getCollection->where( 'name', $state )->first();
$resourceClass = $resourceClass ?: self::getModelResource( $getModel );
// Check if the resource class exists
if ( class_exists( $resourceClass ) ) {
// Generate the URL for the "view" action on the resource
$url = $resourceClass::getUrl( $page, [ $getModel->id ] );
// You can use the generated URL in your link
return new HtmlString( "<a href='{$url}'{$newTab}>{$getModel->$columnText}</a>" );
} else {
// Handle if the resource class does not exist
return new HtmlString( "<span>{$getModel->$columnText}</span>" );
}
} else {
$output = '';
// Loop through the related records
foreach ( $getCollection as $relation ) {
$resourceClass = self::getModelResource( $relation );
// Check if the resource class exists for the related model
if ( class_exists( $resourceClass ) ) {
// Generate the URL for the "view" page of the relation
$url = $resourceClass::getUrl( $page, [ $relation->id ] );
// Append the link for the relation to the output
$output .= new HtmlString( "<a href='{$url}'{$newTab}>{$relation->$columnText}</a>, ");
} else {
// If the related resource does not exist, just show the relation's name
$output .= new HtmlString( "<span>{$relation->$columnText}</span>, ");
}
}
return new HtmlString( $output );
}
} );
}
public function getModelResource( $model )
{
$modelClass = get_class( $model );
return "App\\Filament\\Resources\\" . class_basename( $modelClass ) . 'Resource';
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've extended the TextColumn/TextEntry and added a feature to include a link if the data has a resource. Hopefully, this can be added to Filament as a default option, but feel free to use it in the meantime.
`<?php
namespace App\Extended\Tables\Columns;
use Filament\Tables\Columns\TextColumn as ExtendedTextColumn;
use Illuminate\Support\HtmlString;
class TextColumn extends ExtendedTextColumn
{
}`
Beta Was this translation helpful? Give feedback.
All reactions