-
app/Exports/FormSubmissionExport.php
Open in GitHubuse Maatwebsite\Excel\Concerns\FromArray; use Maatwebsite\Excel\Concerns\WithHeadingRow; class FormSubmissionExport implements FromArray, WithHeadingRow { protected array $submissionData; public function __construct(array $submissionData) { $headingRow = []; $contentRow = []; foreach ($submissionData as $i => $row) { if($i==0){ $headingRow[] = array_keys($row); } $contentRow[] = array_values($row); } $this->submissionData = [ $headingRow, $contentRow ]; } public function array(): array { return $this->submissionData; } }
-
app/Http/Controllers/Forms/FormSubmissionController.php
Open in GitHubuse App\Http\Controllers\Controller; use App\Models\Forms\Form; use App\Exports\FormSubmissionExport; use App\Service\Forms\FormSubmissionFormatter; use Maatwebsite\Excel\Facades\Excel; class FormSubmissionController extends Controller { // public function export(string $id) { $form = Form::findOrFail((int) $id); $this->authorize('view', $form); $allRows = []; foreach ($form->submissions->toArray() as $row) { $formatter = (new FormSubmissionFormatter($form, $row['data'])) ->outputStringsOnly() ->setEmptyForNoValue() ->showRemovedFields(); $tmp = $formatter->getCleanKeyValue(); $tmp['Create Date'] = date("Y-m-d H:i", strtotime($row['created_at'])); $allRows[] = $tmp; } $csvExport = (new FormSubmissionExport($allRows)); return Excel::download( $csvExport, $form->slug.'-submission-data.csv', \Maatwebsite\Excel\Excel::CSV ); } // }