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();
    }


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');
    }


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),
            ])
     
    }
Was this page helpful?