2017 © Pedro Peláez
 

library exporter

Export a collection of models to csv

image

ralphmorris/exporter

Export a collection of models to csv

  • Monday, November 13, 2017
  • by ralphmorris
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel CSV Export

Exporter is a package for Laravel that provides a simple csv export of any collection of models., (*1)

Installation

composer require ralphmorris/exporter

Usage

In your controller use:, (*2)

use RalphMorris\Exporter\Exporter;

Then inside your method a simple call could look like:, (*3)

public function export()
{
    $users = User::get();

    $exporter = new Exporter;

    return $exporter->exportToCsv($users);
}

You can also optionally specify the filename by providing a second parameter., (*4)

return $exporter->exportToCsv($users, 'my-file-name.csv');

Speciying which columns to export

If you only want to export certain columns from your model simply include the ExportableColumnsTrait trait in your model class and define a protected property of $exportableColumns with an array of the fields you would like to be exportable., (*5)

For example:, (*6)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use RalphMorris\Exporter\ExportableColumnsTrait;

class User extends Model
{
    use ExportableColumnsTrait;

    /**
     * The columns that are exportable to CSV
     * 
     * @var array
     */
    protected $exportableColumns = [
        'name',
        'email',
    ];

Then in your query simply call the query scope exportableColumns() as per the below example., (*7)

public function export()
{
    $users = User::exportableColumns()->get();

    $exporter = new Exporter;

    return $exporter->exportToCsv($users);
}

The Versions

13/11 2017

dev-master

9999999-dev

Export a collection of models to csv

  Sources   Download

MIT

The Development Requires

by Ralph Morris

laravel csv export

13/11 2017

1.0.0

1.0.0.0

Export a collection of models to csv

  Sources   Download

MIT

The Development Requires

by Ralph Morris

laravel csv export