Download the PHP package kakposoe/laravel-csv-to-model without Composer
On this page you can find all versions of the php package kakposoe/laravel-csv-to-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kakposoe/laravel-csv-to-model
More information about kakposoe/laravel-csv-to-model
Files in kakposoe/laravel-csv-to-model
Package laravel-csv-to-model
Short Description Eloquent helper to import csv data directly into a model
License MIT
Homepage https://github.com/kakposoe/laravel-csv-to-model
Informations about the package laravel-csv-to-model
Laravel Csv To Model
Helper for Laravel Eloquent to import csv data directly into a model.
Behind the scenes, the package wraps the box/spout package to iterate the imported csv.
Installation
Via Composer
Usage
You must use the CsvToModel
trait in the model you would like to import csv data:
The package offers an expressive api to prepare and process an entire csv import into the database via an eloquent command.
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:
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:
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:
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.