Skip to content

Commit

Permalink
Export selected result records (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen authored Jan 29, 2023
1 parent cf1d73d commit 84759a2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/Exports/ResultsSelectedBulkExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ResultsSelectedBulkExport implements FromArray, WithHeadings
{
protected $results;

public function __construct(array $results)
{
$this->results = $results;
}

public function array(): array
{
return $this->results;
}

public function headings(): array
{
return [
'id',
'ping',
'download',
'upload',
'server id',
'server url',
'server name',
'result url',
'scheduled',
'successful',
'data',
'created at',
];
}
}
11 changes: 11 additions & 0 deletions app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources;

use App\Exports\ResultsSelectedBulkExport;
use App\Filament\Resources\ResultResource\Pages;
use App\Models\Result;
use App\Settings\GeneralSettings;
Expand All @@ -16,6 +17,8 @@
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Facades\Excel;

class ResultResource extends Resource
{
Expand Down Expand Up @@ -167,6 +170,14 @@ public static function table(Table $table): Table
]),
])
->bulkActions([
Tables\Actions\BulkAction::make('export')
->label('Export selected')
->icon('heroicon-o-download')
->action(function (Collection $records) {
$export = new ResultsSelectedBulkExport($records->toArray());

return Excel::download($export, 'results_'.now()->timestamp.'.csv', \Maatwebsite\Excel\Excel::CSV);
}),
Tables\Actions\DeleteBulkAction::make(),
])
->defaultSort('created_at', 'desc');
Expand Down

0 comments on commit 84759a2

Please sign in to comment.