public function toCSV(Builder $query)
    {
        $out = fopen(
            $file = $this->getFilename(),
            'a+'
        );
        $headersPrinted = false;

        foreach ($this->each($query, 100) as $item) {
            $data = $this->toScalar($item);

            if (!$headersPrinted) {
                fputcsv($out, array_keys($data));
                $headersPrinted = true;
            }

            fputcsv($out, $data);
        }
        fclose($out);

        return $this->sendDownloadResponse($file, 'csv', ['Content-Type' => 'text/csv']);
    }