F
Filament7mo ago
Pscl

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;
}
}
` I do have a TimeEntries table. I want to create an exporter that exports the time entries grouped by name and it's duration in sum. But Exporter Fails. In my laravel.log file I get the error attached. Any idea how to achieve an grouped export?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server