How to avoid extra queries created when related data is displayed?

The following adds a tooltip to show: "booking xyz has 1 deal" Or, "booking xyz has 2 deals" (implementing Str::plural) with the number of records in the related "deals" table.
Tables\Columns\TextColumn::make('deals_count')->counts('deals')
->color('primary')
->icon('heroicon-o-thumb-up')
->label('Deals')
->tooltip(fn (Booking $record): string => $record->name . " has " . $record->deals()->count() . " " . Str::plural('deal', $record->deals()->count())),
Tables\Columns\TextColumn::make('deals_count')->counts('deals')
->color('primary')
->icon('heroicon-o-thumb-up')
->label('Deals')
->tooltip(fn (Booking $record): string => $record->name . " has " . $record->deals()->count() . " " . Str::plural('deal', $record->deals()->count())),
The above adds two extra queries, how can I still show the icon with the records count but avoid extra queries? Any advice is much appreciated. Thank you.
2 Replies
ByteXR
ByteXR2y ago
Would it not help eager load deals in getTableQuery method and instead of counting on new query, count collection so $record->deals->count()
Pablo Torres
Pablo TorresOP2y ago
@bytexr of course it helps... thank you! Much appreciated...
Want results from more Discord servers?
Add your server