src/Traits/ExportsCollection.php
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']);
    }
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']);
    }
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' ] ) ; }
Convert & download collection in CSV format.

@param $query

@return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
Convert & download collection in CSV format .