From 0f38324f5b633c7059a35cd6006f5641a58dba00 Mon Sep 17 00:00:00 2001 From: George Daneke Date: Wed, 26 Jun 2024 00:15:31 +0300 Subject: [PATCH] show count of items on location widget --- .../Resources/LocationResource/Pages/ViewLocation.php | 1 - app/Filament/Widgets/Locations.php | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/LocationResource/Pages/ViewLocation.php b/app/Filament/Resources/LocationResource/Pages/ViewLocation.php index 7832267..15b546c 100644 --- a/app/Filament/Resources/LocationResource/Pages/ViewLocation.php +++ b/app/Filament/Resources/LocationResource/Pages/ViewLocation.php @@ -3,7 +3,6 @@ namespace App\Filament\Resources\LocationResource\Pages; use App\Filament\Resources\LocationResource; -use Filament\Actions; use Filament\Resources\Pages\ViewRecord; class ViewLocation extends ViewRecord diff --git a/app/Filament/Widgets/Locations.php b/app/Filament/Widgets/Locations.php index 4276344..7a5288b 100644 --- a/app/Filament/Widgets/Locations.php +++ b/app/Filament/Widgets/Locations.php @@ -8,6 +8,7 @@ use Filament\Tables\Columns\Layout\Stack; use Filament\Tables\Table; use Filament\Widgets\TableWidget as BaseWidget; +use Illuminate\Database\Eloquent\Builder; class Locations extends BaseWidget { @@ -19,12 +20,15 @@ public function table(Table $table): Table { return $table ->query(LocationResource::getEloquentQuery()->where('parent_id', null)) + ->modifyQueryUsing(fn (Builder $query) => $query->withCount('items')) ->defaultPaginationPageOption(15) ->defaultSort('created_at', 'desc') ->columns([ Stack::make([ Tables\Columns\TextColumn::make('name') - ->searchable()->sortable(), + ->searchable() + ->suffix(fn (Location $record + ): string => $record->items_count > 0 ? ' ('.$record->items_count.' items)' : ''), ]), ]) ->contentGrid([