Skip to content

Commit

Permalink
import/export eloquent models as csv
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Wilgucki committed Sep 17, 2015
1 parent 066169f commit debbd1c
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 8 deletions.
59 changes: 57 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,64 @@ Don't forget to close file after you're done with your work.

$writer->close();

##Intergating with Eloquent models

If you want/need to integrate CsvReader and/or CsvWriter with Eloquent model, there's a simple way to do this.
The Csv package offers three traits in order to simplify the process. You can use all traits as well as one or two of your choise.

**These traits hasn't been tested for relations.**

###CsvCustomCollection

CsvCustomCollection trait added to model class enables <code>toCsv</code> method that can be used on collection.

use Wilgucki\Csv\Traits\CsvCustomCollection;

class SomeModel extends Model
{
use CsvCustomCollection;
//...
}

$items = SomeModel::all(); // you can use where as well
$csvData = $items->toCsv();

###CsvExportable

CsvExportable trait allows you to convert single model object to CSV data.

use Wilgucki\Csv\Traits\CsvExportable;

class SomeModel extends Model
{
use CsvExportable;
//...
}

$csvData = SomeModel::find(1)->toCsv();

###CsvImportable

CsvImportable trait allows you to import data from CSV file and save it to database.
Imported file must have header line containing column names as they are named in database table. Primary key must be named **id**.
CSV importer will update all rows with matching id and add every row that isn't found in table.

use Wilgucki\Csv\Traits\CsvImportable;

class SomeModel extends Model
{
use CsvImportable;
//...
}

SomeModel::fromCsv('/path/to/file.csv');

##TODO

- tests
- convert encoding (useful when dealing with CSV file generated by Excel)
- export Eloquent models as CSV
- import CSV to Eloquent models
- import/export to CSV with relations
- set default CSV parametrs in config file
21 changes: 21 additions & 0 deletions src/CsvCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Wilgucki\Csv;

use Illuminate\Database\Eloquent\Collection;

class CsvCollection extends Collection
{
public function toCsv()
{
$writer = \CsvWriter::create();
$data = $this->toArray();
if (isset($data[0])) {
$writer->writeLine(array_keys($data[0]));
}
$writer->writeAll($data);
$out = $writer->flush();
$writer->close();
return $out;
}
}
6 changes: 3 additions & 3 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Reader
* Open CSV file for reading
*
* @param $file File name with path to open
* @param null $delimiter @see http://php.net/manual/en/function.fgetcsv.php
* @param null $enclosure @see http://php.net/manual/en/function.fgetcsv.php
* @param null $escape @see http://php.net/manual/en/function.fgetcsv.php
* @param null $delimiter @link http://php.net/manual/en/function.fgetcsv.php
* @param null $enclosure @link http://php.net/manual/en/function.fgetcsv.php
* @param null $escape @link http://php.net/manual/en/function.fgetcsv.php
*
* @return $this
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Traits/CsvCustomCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Wilgucki\Csv\Traits;

use Wilgucki\Csv\CsvCollection;

trait CsvCustomCollection
{
public function newCollection(array $models = [])
{
return new CsvCollection($models);
}
}
17 changes: 17 additions & 0 deletions src/Traits/CsvExportable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Wilgucki\Csv\Traits;

trait CsvExportable
{
public function toCsv()
{
$writer = \CsvWriter::create();
$data = $this->toArray();
$writer->writeLine(array_keys($data));
$writer->writeLine($data);
$out = $writer->flush();
$writer->close();
return $out;
}
}
22 changes: 22 additions & 0 deletions src/Traits/CsvImportable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Wilgucki\Csv\Traits;

trait CsvImportable
{
public static function fromCsv($file)
{
$reader = \CsvReader::open($file);
$reader->getHeader();
while (($row = $reader->readLine()) !== false) {
$model = self::findOrNew($row['id']);
foreach ($row as $column => $value) {
if ($column == 'id') {
continue;
}
$model->{$column} = $value;
}
$model->save();
}
}
}
6 changes: 3 additions & 3 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Writer
* Open CSV file for writing.
*
* @param string $file File name for writing CSV data. If not provided memory will be used as CSV file.
* @param null $delimiter @see http://php.net/manual/en/function.fputcsv.php
* @param null $enclosure @see http://php.net/manual/en/function.fputcsv.php
* @param null $escape @see http://php.net/manual/en/function.fputcsv.php
* @param null $delimiter @link http://php.net/manual/en/function.fputcsv.php
* @param null $enclosure @link http://php.net/manual/en/function.fputcsv.php
* @param null $escape @link http://php.net/manual/en/function.fputcsv.php
*
* @return $this
*/
Expand Down

0 comments on commit debbd1c

Please sign in to comment.