A package to help with loading files into MySQL tables.
This uses MySQL's LOAD DATA statement to load text files quickly into your database.
This is usually 20 times faster than using INSERT statements according to: https://dev.mysql.com/doc/refman/8.0/en/insert-optimization.html
This library currently can handle any of the options in a normal LOAD DATA statement except for the partitioned table support. This will be included in a future release of laravel-loadfile.
Further information on the following options that can be passed to the load file builder can be found here:
https://dev.mysql.com/doc/refman/8.0/en/load-data.html
Requires > PHP 8.1 and > Laravel 9
Older versions of Laravel and PHP are supported through previous major versions of this library
composer require ellgreen/laravel-loadfile
To use local files you will need to have local_infile
enabled for
the client and server. To do this on the Laravel side you will need
to add the following to the options
part of your database config:
'config' => [
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
],
use EllGreen\LaravelLoadFile\Laravel\Facades\LoadFile;
LoadFile::file('/path/to/employees.csv', $local = true)
->into('employees')
->columns(['forename', 'surname', 'employee_id'])
->load();
LoadFile::file('/path/to/employees.csv', $local = true)
->into('employees')
->columns(['forename', 'surname', 'employee_id'])
->ignoreLines(1)
->load();
LoadFile::file('/path/to/employees.csv', $local = true)
->into('employees')
->columns(['forename', 'surname', 'employee_id'])
// like this
->fieldsTerminatedBy(',')
->fieldsEscapedBy('\\\\')
->fieldsEnclosedBy('"')
// or
->fields(',', '\\\\', '"')
->load();
LoadFile::file('/path/to/employees.csv', $local = true)
->into('employees')
->columns(['forename', 'surname', 'employee_id'])
// like this
->linesStartingBy('')
->linesTerminatedBy('\\n')
// or
->lines('', '\\n')
->load();
LoadFile::file('/path/to/employees.csv', $local = true)
->into('employees')
->columns([
DB::raw('@forename'),
DB::raw('@surname'),
'employee_id',
])
->set([
'name' => DB::raw("concat(@forename, ' ', @surname)"),
])
->load();
LoadFile::connection('mysql')
->file('/path/to/employees.csv', $local = true)
->into('employees')
->columns(['forename', 'surname', 'employee_id'])
->load();
LoadFile::connection('mysql')
->file('/path/to/employees.csv', $local = true)
->replace()
// or
->ignore()
->into('employees')
->load();
Simply add the LoadsFiles
trait to your model like so:
use EllGreen\LaravelLoadFile\Laravel\Traits\LoadsFiles;
class User extends Model
{
use LoadsFiles;
}
Then you can use the following method to load a file into that table:
User::loadFile('/path/to/users.csv', $local = true);
Add the following method to your Model
class User extends Model
{
use LoadsFiles;
public function loadFileOptions(Builder $builder): void
{
$builder
->fieldsTerminatedBy(',')
->ignoreLines(1);
}
}
User::loadFileBuilder($file, $local)
->replace()
->ignoreLines(1)
->load();
composer test-unit
You will need to have docker installed for these.
composer check