Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me
Export Csv

Introduction

Export arrays to CSV

https://github.com/dcblogdev/exportcsv

Include the composer autoloader, import the ExportCsv namespace.

use Dcblogdev\ExportCsv\ExportCsv;

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

//set column names
$headerFields = ['First Name', 'Last Name', 'Company', 'Created'];

//create array
$records = [];

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

//OR set an array manually
$records[] = ['name', 'last name', 'comy', 'created'];

//send params to csv
new csv($records, $filename, $headerFields);
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.

Learn Laravel with Laracasts

Faster Laravel Hosting

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