-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import/export eloquent models as csv
- Loading branch information
Maciej Wilgucki
committed
Sep 17, 2015
1 parent
066169f
commit debbd1c
Showing
7 changed files
with
136 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters