项目作者: bayareawebpro

项目描述 :
A simple CSV importer / exporter for Laravel Framework that supports Lazy Collections and Stream Downloads
高级语言: PHP
项目地址: git://github.com/bayareawebpro/laravel-simple-csv.git
创建时间: 2018-07-29T14:07:39Z
项目社区:https://github.com/bayareawebpro/laravel-simple-csv

开源协议:MIT License

下载


Laravel Simple CSV





https://packagist.org/packages/bayareawebpro/laravel-simple-csv

Features

  • Import to LazyCollection.
  • Export from Collection, LazyCollection, Iterable, Generator, Array.
  • Low(er) Memory Consumption by use of LazyCollection Generators.
  • Uses Native PHP SplFileObject.
  • Facade Included.

Installation

Require the package and Laravel will Auto-Discover the Service Provider.

  1. composer require bayareawebpro/laravel-simple-csv

Usage:

Invokable classes can be passed to the import method allowing you to customize
how each row is processed. Two classes to handle numerics
and null values have been supplied.

  1. use BayAreaWebPro\SimpleCsv\SimpleCsv;
  2. use BayAreaWebPro\SimpleCsv\Casts\EmptyValuesToNull;
  3. use BayAreaWebPro\SimpleCsv\Casts\NumericValues;
  4. $lazyCsvCollection = SimpleCsv::import(storage_path('collection.csv'), [
  5. EmptyValuesToNull::class,
  6. NumericValues::class,
  7. ]);

Invokable Classes

Dependency Injection: Invokable classes can typehint required dependencies in a
constructor method when defined.

  1. <?php declare(strict_types=1);
  2. namespace App\Csv\Casts;
  3. use Carbon\Carbon;
  4. class Timestamps
  5. {
  6. /** Invoked for each row in import collection. */
  7. public function __invoke(array $item): array
  8. {
  9. foreach ($item as $key => $value){
  10. if(in_array($key, ['created_at', 'updated_at'])){
  11. $item[$key] = Carbon::parse($value);
  12. }
  13. }
  14. return $item;
  15. }
  16. }

Export to File

  1. use BayAreaWebPro\SimpleCsv\SimpleCsv;
  2. // Collection
  3. SimpleCsv::export(
  4. Collection::make(...),
  5. storage_path('collection.csv')
  6. );
  7. // LazyCollection
  8. SimpleCsv::export(
  9. LazyCollection::make(...),
  10. storage_path('collection.csv')
  11. );
  12. // Generator (Cursor)
  13. SimpleCsv::export(
  14. User::query()->where(...)->limit(500)->cursor(),
  15. storage_path('collection.csv')
  16. );
  17. // Array
  18. SimpleCsv::export(
  19. [...],
  20. storage_path('collection.csv')
  21. );

Export Download Stream

  1. use BayAreaWebPro\SimpleCsv\SimpleCsv;
  2. return SimpleCsv::download([...], 'download.csv');

Override Options

  1. use Illuminate\Support\Facades\Config;
  2. Config::set('simple-csv.delimiter', ...);
  3. Config::set('simple-csv.enclosure', ...);
  4. Config::set('simple-csv.escape', ...);

Or, Create a Config File

config/simple-csv.php

  1. return [
  2. 'delimiter' => '?',
  3. 'enclosure' => '?',
  4. 'escape' => '?',
  5. ];

File Splitting Utility

A file splitting utility has been included that will break large CSV files into chunks
(while retaining column headers) which you can move/delete after importing.
This can help with automating the import of large data sets.

Tip: Find your Bash Shell Binary Path: which sh

  1. /bin/sh vendor/bayareawebpro/laravel-simple-csv/split-csv.sh /Projects/laravel/storage/big-file.csv 5000
  2. File Output:
  3. /Projects/laravel/storage/big-file-chunk-1.csv (chunk of 5000)
  4. /Projects/laravel/storage/big-file-chunk-2.csv (chunk of 5000)
  5. /Projects/laravel/storage/big-file-chunk-3.csv (chunk of 5000)
  6. etc...

Speed Tips

  • Using Lazy Collections is the preferred method.
  • Using the queue worker, you can import a several thousand rows at a time without much impact.
  • Be sure to use “Database Transactions” and “Timeout Detection” to insure safe imports.
  • @danielalvidrez/laravel-query-builder-macros-fe176d34135e">Article: How to Insert & Update Many at Once