Modifying exporter to get grouped exports
<?php
namespace App\Filament\Exports;
use App\Models\TimeEntry;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class TimeEntryExporter extends Exporter
{
protected static ?string $model = TimeEntry::class;
public static function getColumns(): array
{
return [
ExportColumn::make('task_name'),
ExportColumn::make('total_duration_hours'),
];
}
public static function modifyQuery(Builder $query): Builder
{
return $query->select('task_name', DB::raw('SUM(duration_hours) as total_duration_hours'))
->groupBy('task_name');
}
public static function getCompletedNotificationBody(Export $export): string
{
$body = 'Your time entry export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';
if ($failedRowsCount = $export->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
}
return $body;
}
}
<?php
namespace App\Filament\Exports;
use App\Models\TimeEntry;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class TimeEntryExporter extends Exporter
{
protected static ?string $model = TimeEntry::class;
public static function getColumns(): array
{
return [
ExportColumn::make('task_name'),
ExportColumn::make('total_duration_hours'),
];
}
public static function modifyQuery(Builder $query): Builder
{
return $query->select('task_name', DB::raw('SUM(duration_hours) as total_duration_hours'))
->groupBy('task_name');
}
public static function getCompletedNotificationBody(Export $export): string
{
$body = 'Your time entry export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';
if ($failedRowsCount = $export->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
}
return $body;
}
}
0 Replies