© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•8mo ago•
3 replies
moinmichi

Searchable column with scoped model

I've got a table on a custom page which uses a model with a scope for its query.
This is the scope:
public function scopeCalculatedClassFrequencies(Builder $query, string|null $name = null): void
    {
        $query->select(
            $this->getTable() . '.id',
            DB::raw('"' . __('Zügigkeiten') . '" as name'),
            DB::raw('LEFT(j.titel, 4) as year'),
            DB::raw('SUM(cf.zuegigkeit) AS calculated_value'),
            $this->getTable() . '..number AS expected_value',
        )
            ->where('reference', 'class-frequencies')
            ->join(app(Year::class)->getTable() . ' as j', DB::raw('LEFT(j.titel, 4)'), '=',  $this->getTable() . '.year')
            ->join(app(ClassFrequency::class)->getTable() . ' as cf', 'cf.jahrgang', '=', 'j.id')
            ->groupBy(DB::raw('LEFT(j.titel, 4)'))
            ->orderBy('year')
    }
public function scopeCalculatedClassFrequencies(Builder $query, string|null $name = null): void
    {
        $query->select(
            $this->getTable() . '.id',
            DB::raw('"' . __('Zügigkeiten') . '" as name'),
            DB::raw('LEFT(j.titel, 4) as year'),
            DB::raw('SUM(cf.zuegigkeit) AS calculated_value'),
            $this->getTable() . '..number AS expected_value',
        )
            ->where('reference', 'class-frequencies')
            ->join(app(Year::class)->getTable() . ' as j', DB::raw('LEFT(j.titel, 4)'), '=',  $this->getTable() . '.year')
            ->join(app(ClassFrequency::class)->getTable() . ' as cf', 'cf.jahrgang', '=', 'j.id')
            ->groupBy(DB::raw('LEFT(j.titel, 4)'))
            ->orderBy('year')
    }

The table uses this scope as the query and a searchable name column. Because of the scope I use a callback function to add the search condition to the query:
$table->query(ImportExpectation::CalculatedClassFrequencies())
->columns([
                TextColumn::make('name')
                    ->searchable(isGlobal: false, isIndividual: true, query: function(Builder $query, string $search) {
                        $query->having('name', 'like', '%' . $search . '%');
                }),
$table->query(ImportExpectation::CalculatedClassFrequencies())
->columns([
                TextColumn::make('name')
                    ->searchable(isGlobal: false, isIndividual: true, query: function(Builder $query, string $search) {
                        $query->having('name', 'like', '%' . $search . '%');
                }),

Unfortunately the search doesn't apply to the data. The table shows that a filter is active with the search string, but still all data is shown. Any idea what I'm doing wrong?
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Searchable translatable column
FilamentFFilament / ❓┊help
2y ago
problem with customized searchable text column
FilamentFFilament / ❓┊help
2y ago
searchable in id column
FilamentFFilament / ❓┊help
3y ago
Searchable not working in column
FilamentFFilament / ❓┊help
3y ago