Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Processing a csv file in Laravel

From time to time I need to process a csv file. PHP provides a fgetcsvfunction to help with that task. Unfortunately this is a very basic function. It will not, for instance, recognize a header column as such. In this quick post I’ll show how using the excellent laravel-excel package (which can process csv’s too) can help making your code much more readable.

Image you’ve got this csv than you want to import in your database:

Clegane,Sandor,Axe Wielder
Bolton,Ramsay,Hound Master
Snow, Jon, King Of The North

Without using laravel-excel you could do this:

$handle = fopen('characters.csv', "r");
$header = true;

while ($csvLine = fgetcsv($handle, 1000, ",")) {

    if ($header) {
        $header = false;
    } else {
            'name' => $csvLine[0] . ' ' . $csvLine[1],
            'job' => $csvLine[2],

Let’s see what we have on our hands: a file handle, a boolean to help skip the headers, numeric indexes on the $csvLine variable, an ugly loop, and an else branch. What a mess.

Let’s improve this immensely by using laravel-excel:

use Illuminate\Support\Collection;

Excel::load('characters.csv')->each(function (Collection $csvLine) {

         'name' => "{$csvLine->get('first_name')} {$csvLine->get('last_name')}",
         'job' => $csvLine->get('job'),


To my eyes this is much cleaner. We don’t need to manage the crap mentioned above anymore. The load function takes care of reading the file and processing the headers. It’ll return a Collection object that you know and love.

The only advantage of the first example is that it uses less memory. The csvfile never gets entirely pulled in to memory, but only processed line by line. In the second example the whole csvfile gets loaded into memory at once. But laravel-excel has you covered. When dealing with big files you it can process the data in chucks, so only a few records are in memory at any given time. Read the docs on importing to learn how to do that.

You might argue that it’s overkill to pull in a package just to read csv’s. I think the added readability (and therefore improved maintainability) of the code is well worth the price of adding a package to a project.

EDIT: another library you might use to import csv’s:

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.