FilamentF
Filament15mo ago
nathan

Table and getting counts of records (groupBy)

I have a table widget. It generates a list of mobile device models and the number of mobiles of that given model. In doing this I can't search or sort because the query isn't something filament can modify for searching and sorting. I get the 'column: count doesn't exist`. Is there a "Filament" approved way of accomplishing this?
This is my widget:
class SolarwindsMobileByModelTable extends BaseWidget
{

    public function getTableRecordKey(Model $record): string
    {
        return 'model';
    }

    public function table(Table $table): Table
    {
        return $table
            ->query(
                SolarwindsMobile::selectRaw('model, COUNT(*) as count')
                    ->where('device_type', 'iPhone')
                    ->orWhere('device_type', 'Phone')
                    ->groupBy('model')
                    ->orderBy('model')
            )
            ->columns([
                Tables\Columns\TextColumn::make('model')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('count')
                    ->sortable()
            ]);
    }

See the image for an example of how it looks. Is this the proper way of accomplishing this?
image.png
Was this page helpful?