Export to CSV

David Carr

Development

Today I've released a new PHP package for exporting array data to a CSV file. Often data needs exporting from a database into CSV files. 

To install the package import it with composer:

composer require daveismyname/exportcsv

Then to use it import the namespace

use Daveismyname\ExportCsv\ExportCsv;

To export the data the following is required

//set filename
$filename = 'test.csv';

//set column names
$headerFields = array('First Name', 'Last Name', 'Company', 'Created');

//create array
$records = [];

//loop through data and add to array
foreach($contacts as $row) {
    $records[] = array(
        $row->firstName, 
        $row->lastName, 
        $row->companyName, 
        $row->created_at
    );
}

//send params to csv 
new csv($records, $filename, $headerFields);

More details can be found at https://github.com/daveismyname/exportcsv

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.