user(); if ($request->has('send_email') && $request->get('send_email')) { SendToAdmin::dispatch($user->company(), $request->all(), ContactExport::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(), ContactExport::class, $hash); return response()->json(['message' => $hash], 200); } // expect a list of visible fields, or use the default $export = new ContactExport($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); } }