Giter VIP home page Giter VIP logo

csv-collection's Introduction

CSV Collection

Latest Version on Packagist Total Downloads

This package provides a simple but powerful way to read and write large CSV files using the power of Laravel's lazy collections.

Installation

You can install the package via composer:

composer require dutchcodingcompany/csv-collection

Usage

You may create a collection using the new keyword or the make method.

CsvCollection::make();

This gives you access to all Collection and Lazy Collection methods.

Open

To open a file and load it's content into a new collection you may use the open method on the collection.

use DutchCodingCompany\CsvCollection\CsvCollection;

CsvCollection::make()
    ->open('path/to/file.csv')
    ->count();

Save

To save the collection items to a file you may use the save method on the collection.

use DutchCodingCompany\CsvCollection\CsvCollection;

CsvCollection::make(static function () {
    yield [
        'key' => 'value',
    ];
})
    ->save('path/to/file.csv');

Model exports

When exporting models a memory efficient method is to lazily iterate through the models and yield it's content.

use DutchCodingCompany\CsvCollection\CsvCollection;

CsvCollection::make(static function () {
    $models = Model::query()->lazy();
    
    foreach ($models as $model){
        yield $model->only([
            'id',
            //
        ]);
    }
})
    ->save('path/to/file.csv');

Options

The following options are available to suit your needs:

  • header, default: true
  • delimiter, default: ,
  • enclosure, default: "
  • escape, default: \\

These options could be passed to the open and save methods, be set using the options method, or be set as the global default using the static defaults method.

The delimiter can be detected for a file by using the detectDelimiter method like this:

CsvCollection::detectDelimiter($path);

Header

When using a header, lines will contain an associated array. Otherwise, lines will contain an indexed array.

// Without header
[
    0 => 'John',
    1 => 'Doe',
]

// With header
[
    'first_name' => 'John',
    'last_name' => 'Doe',
]

Note: When saving a collection to a file the keys of the first element in the collection will be used as the header.

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.

csv-collection's People

Contributors

bjorn-dcc avatar bjornvoesten avatar marcoboers avatar bramr94 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.