F
Filament3mo 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()
]);
}
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?
No description
3 Replies
nathan
nathanOP3mo ago
Nope. If you look at the eloquent statement in my post, you'll see that I'm not dealing with a relationship count. The model column is not a relationship to another table. In my table of mobile devices, there is a column 'model'. I want to groupBy that model string in the mobile table and display the count of records that match that string. Perhaps there is a way to use the filament count() method to achieve what I need? If so, I misunderstood your answer and could use a little clarification. Thank you!
Matthew
Matthew3mo ago
Ok. I've seen people manipulate collections and make the resource utilise those. Personally though I create database views for this sort of thing, and then as far as filament\laravel are concerned, its just a table. Obviously don't try anything other than reading it, but assume for a widget, you wouldn't need to.
Want results from more Discord servers?
Add your server