sorting by polymorphed parent's field

hello everyone. i have a model called SubCluster which can be morphed into several models like this -
public function subclusterable()
{
return $this->morphTo();
}
public function subclusterable()
{
return $this->morphTo();
}
now, for example, a Cluster (one of the potential parents) model is morphedMany to subcluster like this -
public function subClusters()
{
return $this->morphMany(SubCluster::class, 'subclusterable');
}
public function subClusters()
{
return $this->morphMany(SubCluster::class, 'subclusterable');
}
now, in filament, i want to be able to sort by the parent's title as well -
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('title')
->sortable(true, function (Builder $query, string $direction) {
$query->orderByRaw("JSON_UNQUOTE(JSON_EXTRACT(title, '$.en')) $direction");
})
->searchable(query: function (Builder $query, string $search): Builder {
return $query
->where('title', 'like', "%{$search}%")
->orWhereRaw('LOWER(`title`) LIKE LOWER(?)', ["%{$search}%"]);
}),
Tables\Columns\TextColumn::make('subclusterable.title')
->label('Parent'),
Tables\Columns\TextColumn::make('author.name')
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])

}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('title')
->sortable(true, function (Builder $query, string $direction) {
$query->orderByRaw("JSON_UNQUOTE(JSON_EXTRACT(title, '$.en')) $direction");
})
->searchable(query: function (Builder $query, string $search): Builder {
return $query
->where('title', 'like', "%{$search}%")
->orWhereRaw('LOWER(`title`) LIKE LOWER(?)', ["%{$search}%"]);
}),
Tables\Columns\TextColumn::make('subclusterable.title')
->label('Parent'),
Tables\Columns\TextColumn::make('author.name')
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])

}
1 Reply
arbuzik
arbuzik7mo ago
Simply adding a ->sortable() to the parent's field like this -
Tables\Columns\TextColumn::make('subclusterable.title')
->label('Parent')
->sortable(true, function (Builder $query, string $direction) {
$query->orderByRaw("JSON_UNQUOTE(JSON_EXTRACT(subclusterable.title, '$.en')) $direction");
});
Tables\Columns\TextColumn::make('subclusterable.title')
->label('Parent')
->sortable(true, function (Builder $query, string $direction) {
$query->orderByRaw("JSON_UNQUOTE(JSON_EXTRACT(subclusterable.title, '$.en')) $direction");
});
results in an error -
[previous exception] [object] (PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subclusterable.title' in 'order clause' at /Users/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416)
[stacktrace]
[previous exception] [object] (PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subclusterable.title' in 'order clause' at /Users/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416)
[stacktrace]
how can i achieve that please?