CSV Data Manipulation with Laravel

Submitted by lstables - 9 years ago

Using this package - http://csv.thephpleague.com/ In your controller or wherever you are calling this method from simply create a method like so: At the top you need to use: use League\Csv; That's about it, you should be good to go! Would like to thank @msurguy for his help on this too. - See more at: http://laravelsnippets.com/members/snippets/csv-data-manipulation-with-laravel#sthash.oqBx1dzA.dpuf

public function postUpload()
{
$csv = new Reader('path/to/your/file');
$csv->setOffset(1); //because we don't want to insert the header
$nbInsert = $csv->each(function ($row)) {
DB::table('users')->insert(
array(
'firstname' => $row[0],
'lastname' => $row[1],
'email' => $row[2],
)
);
});
return true;
});
}