json(['message' => 'Processing'], 200); $company = auth()->user()->company(); $header = ['first name', 'last name', 'email']; $this->keys = $request->input('keys'); //load the CSV document from a string $this->csv = Writer::createFromString(); //insert the header $this->csv->insertOne($header); $records = []; //insert all the records // $this->csv->insertAll($records); Client::with('contacts')->where('company_id') ->where('is_deleted',0) ->cursor() ->each(function ($client){ // $row = }); echo $this->csv->toString(); } }