user(); if ($request->has('send_email') && $request->get('send_email')) { SendToAdmin::dispatch($user->company(), $request->all(), RecurringInvoiceExport::class, $this->filename); return response()->json(['message' => 'working...'], 200); } // expect a list of visible fields, or use the default if($request->has('output') && $request->input('output') == 'json') { $hash = \Illuminate\Support\Str::uuid(); PreviewReport::dispatch($user->company(), $request->all(), RecurringInvoiceExport::class, $hash); return response()->json(['message' => $hash], 200); } $export = new RecurringInvoiceExport($user->company(), $request->all()); $csv = $export->run(); $headers = [ 'Content-Disposition' => 'attachment', 'Content-Type' => 'text/csv', ]; return response()->streamDownload(function () use ($csv) { echo $csv; }, $this->filename, $headers); } }