PHPackages                             rhuett/csvie - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. rhuett/csvie

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

rhuett/csvie
============

Csvie is a simple CSV file parser made for Laravel. Csvie is based on LeagueCSV, and can quickly import data to, and export data from, a MySQL database. It also gives you a handy abstract class for quickly sanitizing and scrubbing your CSV files prior to insertion.

1.0.3(5y ago)060MPL-2.0PHPCI failing

Since Aug 15Pushed 5y ago1 watchersCompare

[ Source](https://github.com/im-ryan/Csvie)[ Packagist](https://packagist.org/packages/rhuett/csvie)[ Docs](https://github.com/rhuett/csvie)[ RSS](/packages/rhuett-csvie/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)Dependencies (9)Versions (5)Used By (0)

Csvie
=====

[](#csvie)

Csvie is a simple CSV file parser made for Laravel 7. Csvie is based on LeagueCSV, and can quickly import data to, and export data from, a MySQL database. It also gives you a handy abstract class for quickly sanitizing and scrubbing your CSV files prior to insertion.

How it works
------------

[](#how-it-works)

Csvie is meant to quickly load CSV files with more than a few thousand rows of data into a MySQL database. The idea behind how this works is simple:

1. You upload the CSV files onto your server.
2. Use Csvie to chunk the files into smaller pieces. Chunking will be done by rows of data, instead of file globs.
3. Write a custom CSV scrubber to clean data from the chunked files, then overwrite these files on the server.
    1. Note that you do not have to use the included HashCsvCleaner implementation. You are free to write your own using the Rhuett\\Csvie\\Contracts\\CsvieCleaner interface.
4. Directly load the clean files into your MySQL database directly using the [Load Data statement](https://dev.mysql.com/doc/refman/8.0/en/load-data.html).

Installation
------------

[](#installation)

Via Composer:

```
$ composer require rhuett/csvie
$ php artisan vendor:publish --provider="Rhuett\Csvie\CsvieServiceProvider"
```

Make sure to add the following line to your app/config/database.php file:

```
'mysql' => [
    'driver' => 'mysql',
    // ...
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        // ...
        PDO::MYSQL_ATTR_LOCAL_INFILE => true,
    ]) : [],
],
```

Once you have finished these configuration changes, don't forget to run:

```
$ php artisan config:cache
```

Note that if you are getting errors about this not being enabled on both the client and server side, then you may also need to edit mysql.cnf:

```
# File Location: /etc/mysql/mysql.conf.d/mysql.cnf
# Add the following to the bottom of the file:
[server]
local_infile=true

```

...and run:

```
$ sudo systemctl restart mysql
```

Usage
-----

[](#usage)

### Single CSV file import example, using a controller's store method:

[](#single-csv-file-import-example-using-a-controllers-store-method)

```
public function store(Request $request)
{
    $csvie = new Csvie;                 // note: you can pass an array of config overrides if needed
    $modelInstance = new Model;
    $referenceData = 'Whatever I want'; // note: reference data is optional

    // Note: You can pass an array for both column and model UIDs if you need to verify against multiple columns
    $cleaner = new ModelCleaner(
        'ID',           // column name from CSV file to match
        'model_id',     // model ID to verify against column name
        $modelInstance,
        $referenceData
    );

    $fileName = $request->file->store('/', 'uploads'); // move uploaded file from /temp into permanent storage
    $chunkedFiles = $csvie->chunkFiles(
        $csvie->getStorageDiskPath('uploads').$fileName
    );

    foreach($chunkedFiles as $chunk) {
        $chunkData = $csvie->readCsvFile($chunk);
        $cleanData = $cleaner->scrub($chunkData);
        $cleanFile = $csvie->saveCsvFile($chunk, $cleanData);

        $csvie->importCSV($cleanFile, $modelInstance);
    }
    $csvie->clearStorageDisk(); // clear out leftover uploaded file along with its chunks

    return view('view.index')->with([
        'models' => Model::all()
    ]);
}
```

### Making your own Csvie scrubber:

[](#making-your-own-csvie-scrubber)

Simply run:

```
$ php artisan make:cleaner ModelNameCleaner
```

...and you should get a new file that looks like the one below in the App\\Services\\CsvCleaners directory. Note that the extra comments displayed here will not be included in newly generated files.

```
