定制 dutchcodingcompany/csv-collection 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dutchcodingcompany/csv-collection

最新稳定版本:1.4.0

Composer 安装命令:

composer require dutchcodingcompany/csv-collection

包简介

Read and write large csv files using Laravel's Illuminate Collections

README 文档

README

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.

统计信息

  • 总下载量: 6.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固