Download the PHP package isaacdew/load-data without Composer
On this page you can find all versions of the php package isaacdew/load-data. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download isaacdew/load-data
More information about isaacdew/load-data
Files in isaacdew/load-data
Package load-data
Short Description Fluent syntax for loading large CSV files into MySQL using LOAD DATA INFILE in Laravel.
License MIT
Informations about the package load-data
Introduction
MySQL and MariaDB come equipped with the LOAD DATA INFILE statement which allows for loading large datasets from a CSV or similar file into a table very quickly. This package provides an API for constructing and executing a LOAD DATA INFILE
statement in Laravel.
Installation
Install this package using composer:
composer require isaacdew/load-data
Examples
Basic Example
Ignoring the CSV Header
Defining Columns
Using the Headers from the CSV to Define Your Columns
By default, the CSV headers are converted to snake case since the columns need to match your database table column names. If you need to do any custom modification, you can pass a callback to the useFileHeaderForColumns
method.
Only Loading Specific Columns from the CSV
Truncating the Table Before Load
Using Set Statements
To use this feature, you must define the columns first either with the columns
method or by using the file header with the useFileHeaderForColumns
method. Then you can modify the value from your CSV using a MySQL expression. A good use case is a date column where the CSV isn't using a MySQL friendly format. Note that you must prefix the column name with @
to use it in your expression.
Dedicated Database Servers
If your Laravel application is not on the same server as your database, you will have to make sure the LOAD DATA LOCAL INFILE statement is enabled on your database server and in PDO. This package will automatically use the LOCAL
keyword if your DB_HOST
is not set to 127.0.0.1
or localhost
.
To enable LOAD DATA LOCAL INFILE
in PDO, go to your config/database.php
file and add PDO::MYSQL_ATTR_LOCAL_INFILE => true
to the array of options for the mysql
connection like so:
Note On Security
Prepared statements are not supported for LOAD DATA INFILE
statements. With that being the case, do not use user input for constructing a LOAD DATA INFILE
statement. I took the precaution of using PDO::quote()
to escape the filename, however, I would still recommend against the use of a user provided filename in this statement.