Download the PHP package langleyfoxall/eloquent-csv-importer without Composer
On this page you can find all versions of the php package langleyfoxall/eloquent-csv-importer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download langleyfoxall/eloquent-csv-importer
More information about langleyfoxall/eloquent-csv-importer
Files in langleyfoxall/eloquent-csv-importer
Package eloquent-csv-importer
Short Description Easily map CSV files to Eloquent models
License MIT
Informations about the package eloquent-csv-importer
💾 Eloquent CSV Importer
Eloquent CSV Importer helps create and store column maps to enable the easy conversion of CSV data to Eloquent models
Installation
Eloquent CSV Importer can be easily installed using Composer. Just run the following command from the root of your project.
The service provider is to set to be auto discovered in Laravel 5.5+ - in older versions you will have to manually register it in config/app.php
After this, publish the vendor files to copy the CSV definitions migrations to your migrations folder.
Documentation
Getting started
-
Publish the migrations, add any additional columns to the csv_defintions table you may want & migrate.
-
Add the
CSVMappable
&HasCSVDefinitions
traits to the model you want to store CSV definitions agaisnt. -
Specify the columns that you want to be allowed to be mapped by overriding the
getCSVMappableColumns()
function, by default it uses all the columns of the model. -
Create a CSV definition for your model you want to map to
- You can now use the created definition to convert CSV files / strings into the related models.
Updating by columns
Eloquent CSV Importer allows not only the creation of models from a CSV but the updating of models based on specified keys in the CSV.
Lets say you have the follow CSV:
product ID | price |
---|---|
product-001 | 12.99 |
product-002 | 13.99 |
And the following code to map your CSV file into your models
The above code will now update or create a product model by finding a product where product_name
is equal to the value in the current CSV row or by creating a new one with the data in the current CSV row if one doesn't already exist.
You can pass in multiple keys to update like so:
This will update an existing product where both values in the product are equal to the specified values in the CSV row.
Data item manipulation
If you wish, you can manipulate the data as it is being imported from the CSV. To do this, you must create a new a data item manipulator function, and then set it against the CSV definition.
The data item manipulator function will receive the data item key, value, and a copy of the entire row from the CSV. After making changes, it should return the modified data item value.
The example below converts any value it receives to uppercase.
The data item manipulator should be set before you call the makeModels
or createModels
methods.
All versions of eloquent-csv-importer with dependencies
illuminate/support Version >=5.0
illuminate/database Version >=5.0
illuminate/http Version >=5.0
league/csv Version ^9.1