PHPackages                             chrgriffin/collection-macro-csv - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. chrgriffin/collection-macro-csv

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

chrgriffin/collection-macro-csv
===============================

Laravel Collection macro: convert a collection to a CSV-ready array or string.

v1.0.0(6y ago)11MITPHPPHP ^7.1.3CI failing

Since Mar 3Pushed 6y ago1 watchersCompare

[ Source](https://github.com/ChrGriffin/collection-macro-csv)[ Packagist](https://packagist.org/packages/chrgriffin/collection-macro-csv)[ RSS](/packages/chrgriffin-collection-macro-csv/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

[![Build Status](https://camo.githubusercontent.com/be4807ae6c6960b7cc270b4c3ac7e0d543f8d5c59caa754cf89b872a3ad9e808/68747470733a2f2f6170702e636f6465736869702e636f6d2f70726f6a656374732f64353664623736302d336639312d303133382d386665392d3765663935613763303136652f7374617475733f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/be4807ae6c6960b7cc270b4c3ac7e0d543f8d5c59caa754cf89b872a3ad9e808/68747470733a2f2f6170702e636f6465736869702e636f6d2f70726f6a656374732f64353664623736302d336639312d303133382d386665392d3765663935613763303136652f7374617475733f6272616e63683d6d6173746572)[![Coverage Status](https://camo.githubusercontent.com/bb6bc23f4a3d709894e8c115c39700f43171ab011ae8ad994d0668b00e084df9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4368724772696666696e2f636f6c6c656374696f6e2d6d6163726f2d6373762f62616467652e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/bb6bc23f4a3d709894e8c115c39700f43171ab011ae8ad994d0668b00e084df9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4368724772696666696e2f636f6c6c656374696f6e2d6d6163726f2d6373762f62616467652e7376673f6272616e63683d6d6173746572)

Laravel Collection Macros: CSV
==============================

[](#laravel-collection-macros-csv)

This package will add two Collection macros to transform your Collection into a CSV-ready array or a CSV string, respectively.

Installation
------------

[](#installation)

Install in your Laravel project via composer:

```
composer install chrgriffin/collection-macro-csv
```

If your version of Laravel supports auto-discovery (versions 5.5 and up), that's it!

For older versions of Laravel, you will need to edit your `config/app.php` file to include the service provider in your providers array:

```
return [
    // ...
    'providers' => [
        // ...
        CollectionMacroCsv\ServiceProvider::class
    ]
];
```

Usage
-----

[](#usage)

You should now be able to chain `->toCsvArray()` and `->toCsvString` on any Collection.

### toCsvArray

[](#tocsvarray)

`->toCsvArray()` will transform a two dimensional Collection of associative arrays into a two dimensional array of 'rows' ready for CSV formatting.

```
$associativeArray = [
    [
        'name' => 'Geralt of Rivia',
        'occupation' => 'Witcher'
    ],
    [
        'name' => 'Yennefer of Vengerberg',
        'occupation' => 'Sorceress'
    ]
];

$csvArray = collect($associativeArray)->toCsvArray();

/*
 * [
 *     ['name', 'occupation'],
 *     ['Geralt of Rivia', 'Witcher'],
 *     ['Yennefer of Vengerberg', 'Sorceress']
 * ]
 */
```

### toCsvString

[](#tocsvstring)

`->toCsvString()` will transform a two dimensional Collection of arrays into a CSV string.

```
$associativeArray = [
    [
        'name' => 'Geralt of Rivia',
        'occupation' => 'Witcher'
    ],
    [
        'name' => 'Yennefer of Vengerberg',
        'occupation' => 'Sorceress'
    ]
];

$csvString = collect($associativeArray)->toCsvString();

// name,occupation\nGeralt of Rivia,Witcher\nYennefer of Vengerberg,Sorceress
```

You can also pass in a custom delimiter if you need to use something other than a comma:

```
$csvString = collect($associativeArray)->toCsvString('|');

// name|occupation\nGeralt of Rivia|Witcher\nYennefer of Vengerberg|Sorceress
```

### Inconsistent Columns

[](#inconsistent-columns)

For Collections of arrays where the array indexes are not consistent with one another, a default value of `null` will be used to 'fill in' missing values:

```
$associativeArray = [
    [
        'name' => 'Geralt of Rivia',
        'occupation' => 'Witcher',
        'witcher_school' => 'Wolf'
    ],
    [
        'name' => 'Yennefer of Vengerberg',
        'occupation' => 'Sorceress',
        'magic_speciality' => 'Portals'
    ]
];

$csvArray = collect($associativeArray)->toCsvArray();

/*
 * [
 *     ['name', 'occupation', 'witcher_school', 'magic_speciality'],
 *     ['Geralt of Rivia', 'Witcher', 'Wolf', null],
 *     ['Yennefer of Vengerberg', 'Sorceress', null, 'Portals']
 * ]
 */

$csvString = collect($associativeArray)->toCsvString();

// name,occupation,witcher_school,magic_speciality\nGeralt of Rivia,Witcher,Wolf,\nYennefer of Vengerberg,Sorceress,,Portals
```

### Structure Requirements

[](#structure-requirements)

The only requirement is that your Collection contain only arrays. If a non-array value is encountered at the first level of the Collection, a `MalformedCollectionException` will be thrown.

```
$malformedArray = [
    [
        'name' => 'Geralt of Rivia',
        'occupation' => 'Witcher',
        'witcher_school' => 'Wolf'
    ],
    'Yennefer of Vengerberg'
];

// throws a MalformedCollectionException
$csvArray = collect($malformedArray)->toCsvArray();
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2258d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/90b61f51551a66c90c131f873fb94ee87a064cb72dc178d8512e90e13dc769e6?d=identicon)[ChrGriffin](/maintainers/ChrGriffin)

---

Top Contributors

[![ChrGriffin](https://avatars.githubusercontent.com/u/10466686?v=4)](https://github.com/ChrGriffin "ChrGriffin (12 commits)")

### Embed Badge

![Health badge](/badges/chrgriffin-collection-macro-csv/health.svg)

```
[![Health](https://phpackages.com/badges/chrgriffin-collection-macro-csv/health.svg)](https://phpackages.com/packages/chrgriffin-collection-macro-csv)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
