Code Monkey home page Code Monkey logo

laravel-csv-to-model's Introduction

Laravel Csv To Model

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

laravel-csv-to-model's People

Contributors

dependabot[bot] avatar kakposoe avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

christiannye

laravel-csv-to-model's Issues

Add ability to update record if similar record is found

For example, finding the same email_address. For each row, it should check if the email is in the database. If found, the id should be retrieved and the row's data should be updated.

Comparison field should not be updated.

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.