PHPackages                             ellgreen/laravel-loadfile - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. ellgreen/laravel-loadfile

ActiveLibrary[Database &amp; ORM](/categories/database)

ellgreen/laravel-loadfile
=========================

A package to help with loading files into MySQL tables

4.0.0(1y ago)82184.9k↑15.2%10[2 PRs](https://github.com/ellgreen/laravel-loadfile/pulls)MITPHPPHP ^8.2CI passing

Since May 3Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ellgreen/laravel-loadfile)[ Packagist](https://packagist.org/packages/ellgreen/laravel-loadfile)[ RSS](/packages/ellgreen-laravel-loadfile/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (15)Used By (0)

Laravel Load File 💽
===================

[](#laravel-load-file-)

[![Latest Stable Version](https://camo.githubusercontent.com/d9d44c7e689d0a216b98880b57022379fd6931bf5a02dc891b40c0b882e6d7c6/68747470733a2f2f706f7365722e707567782e6f72672f656c6c677265656e2f6c61726176656c2d6c6f616466696c652f76)](//packagist.org/packages/ellgreen/laravel-loadfile)[![License](https://camo.githubusercontent.com/5f05bd973fbe22c4090d6d465648fd40f1a9d4b670fda2d9b10bc9de8b3398a2/68747470733a2f2f706f7365722e707567782e6f72672f656c6c677265656e2f6c61726176656c2d6c6f616466696c652f6c6963656e7365)](//packagist.org/packages/ellgreen/laravel-loadfile)

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:

### Options

[](#options)

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:**

Installation
------------

[](#installation)

**Requires &gt;= PHP 8.2 and &gt;= Laravel 12**

*Older versions of Laravel and PHP are supported through previous major versions of this library*

```
composer require ellgreen/laravel-loadfile
```

### Loading files

[](#loading-files)

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,
],
```

#### Simple file import

[](#simple-file-import)

```
use EllGreen\LaravelLoadFile\Laravel\Facades\LoadFile;

LoadFile::file('/path/to/employees.csv', $local = true)
    ->into('employees')
    ->columns(['forename', 'surname', 'employee_id'])
    ->load();
```

#### Ignoring header row

[](#ignoring-header-row)

```
LoadFile::file('/path/to/employees.csv', $local = true)
    ->into('employees')
    ->columns(['forename', 'surname', 'employee_id'])
    ->ignoreLines(1)
    ->load();
```

#### Specifying field options

[](#specifying-field-options)

```
LoadFile::file('/path/to/employees.csv', $local = true)
    ->into('employees')
    ->columns(['forename', 'surname', 'employee_id'])
    // like this
    ->fieldsTerminatedBy(',')
    ->fieldsEscapedBy('\\\\')
    ->fieldsEnclosedBy('"')
    // or
    ->fields(',', '\\\\', '"')
    ->load();
```

#### Specifying line options

[](#specifying-line-options)

```
LoadFile::file('/path/to/employees.csv', $local = true)
    ->into('employees')
    ->columns(['forename', 'surname', 'employee_id'])
    // like this
    ->linesStartingBy('')
    ->linesTerminatedBy('\\n')
    // or
    ->lines('', '\\n')
    ->load();
```

#### Input preprocessing (set)

[](#input-preprocessing-set)

```
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();
```

#### Using a different connection

[](#using-a-different-connection)

```
LoadFile::connection('mysql')
    ->file('/path/to/employees.csv', $local = true)
    ->into('employees')
    ->columns(['forename', 'surname', 'employee_id'])
    ->load();
```

#### Duplicate-key and error handling

[](#duplicate-key-and-error-handling)

```
LoadFile::connection('mysql')
    ->file('/path/to/employees.csv', $local = true)
    ->replace()
    // or
    ->ignore()
    ->into('employees')
    ->load();
```

Loading data into Eloquent Models
---------------------------------

[](#loading-data-into-eloquent-models)

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);
```

### Need to specify options to load the file with?

[](#need-to-specify-options-to-load-the-file-with)

Add the following method to your Model

```
class User extends Model
{
    use LoadsFiles;

    public function loadFileOptions(Builder $builder): void
    {
        $builder
            ->fieldsTerminatedBy(',')
            ->ignoreLines(1);
    }
}
```

### Or you can get an instance of the query builder on the fly

[](#or-you-can-get-an-instance-of-the-query-builder-on-the-fly)

```
User::loadFileBuilder($file, $local)
    ->replace()
    ->ignoreLines(1)
    ->load();
```

Development
-----------

[](#development)

### Check

[](#check)

Runs linting, static analysis, and unit tests.

```
composer check
```

### All tests

[](#all-tests)

Runs unit and feature tests against all support Laravel versions.

**You will need to have [docker](https://www.docker.com/) installed for these.**

```
composer test
```

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance44

Moderate activity, may be stable

Popularity48

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 96.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~127 days

Recently: every ~347 days

Total

12

Last Release

445d ago

Major Versions

0.4.0 → 1.0.02021-05-05

1.2.0 → 2.0.02022-02-16

2.0.0 → 3.0.02023-02-28

3.1.0 → 4.0.02025-02-28

PHP version history (5 changes)0.1.0PHP ^7.4

1.2.0PHP ^7.4|^8.0

2.0.0PHP ^8.0

3.0.0PHP ^8.1

4.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4f85fea6b0835c13393db2e18305160902eab148102b6d6d00a43a9fb22f2d57?d=identicon)[ellgreen](/maintainers/ellgreen)

---

Top Contributors

[![ellgreen](https://avatars.githubusercontent.com/u/17834927?v=4)](https://github.com/ellgreen "ellgreen (58 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")

---

Tags

csvdatabaseinfilelaravelloadload-data-infileloaddatamysqlphptsv

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/ellgreen-laravel-loadfile/health.svg)

```
[![Health](https://phpackages.com/badges/ellgreen-laravel-loadfile/health.svg)](https://phpackages.com/packages/ellgreen-laravel-loadfile)
```

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
