kakposoe/laravel-csv-to-model

Eloquent helper to import csv data directly into a model

1.2 2020-01-02 14:53 UTC

README

Helper for Laravel Eloquent to import csv data directly into a model.

Build Status StyleCI

Behind the scenes, the package wraps the box/spout package to iterate the imported csv.

Installation

Via Composer

$ composer require kakposoe/laravel-csv-to-model

Usage

You must use the CsvToModel trait in the model you would like to import csv data:

<?php

namespace App;

use Kakposoe\CsvToModel\Traits\CsvToModel;

class YourModel extends Model
{
    use CsvToModel;

    ...
}

The package offers an expressive api to prepare and process an entire csv import into the database via an eloquent command.

$csv = Model::csv($path)
$csv->import();

The first argument expects the file path for the csv file. After this, running ->import() will import all rows into the database.

Changing field names

By default, the first row of the csv file will be used to determine the model fields to fill. The field names will be automatically changed to snake_case.

If you need to change the field name, you can use the ->headers() method and pass an array to map the field names:

$csv->headers(['Email Address' => 'email'])

Note: If the field name is not in the array passed into the ->headers() method, it will default to the original, snake_case version of the field.

Specifying fields to import

You can use the ->only() method to specify what fields should be imported:

$csv->only('first_name', 'last_name', 'email', 'contact_number')

Note: If using in conjuction with ->headers(), this would be the mapped name, not the original name.

Formatting values

It is possible to format a value before each insert by using the ->format() method:

$csv->format('first_name', function($value) {
    return strtoupper($value);
})

The first argument is the target field (if field name is changed used ->headers(), use the mapped name). The second argument should be a closure, returning the formatted data.

Credits