Download the PHP package crockett/csv-seeder without Composer

On this page you can find all versions of the php package crockett/csv-seeder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package csv-seeder

CSV Seeder

This package is intended to minimize the time and hassle spent importing CSV-based data. By making a few assumptions about your average CSV file, most users won't need any configuration to start seeding. For those that do, the available configuration options offer plenty of control over your CSV and how the data is inserted into the database.

Overview

Key Features

Installation

Require this package in your composer.json and run composer update

"crockett/csv-seeder": "1.1.*"

Or just require it directly composer require crockett/csv-seeder

Setup

Here is a typical, single CSV seeder setup:

use Crockett\CsvSeeder\CsvSeeder;

class UsersTableSeeder extends CsvSeeder {

    public function __construct()
    {
        $this->filename = base_path('path/to/csv/users.csv');
        $this->table = 'users';
    }

    public function run()
    {
        // runs the seeder - alternatively, you could call $this->runSeeder(); for the same result
        parent::run();
    }
}

If you want to seed multiple CSVs in the same seeder, you could do something like this:

use Crockett\CsvSeeder\CsvSeeder;

class UsersTableSeeder extends CsvSeeder {

    public function run()
    {
        // seed the users table
        $this->filename = base_path('path/to/csv/users.csv');
        $this->table = 'users';
        parent::run();

        // seed the posts table
        $this->filename = base_path('path/to/csv/posts.csv');
        $this->table = 'posts';
        parent::run();
    }
}

As you can imagine, that can get messy very fast. Instead, you could use the helper method seedFromCSV() which is just a cleaner way to define your parameters and run the seeder in one go:

use Crockett\CsvSeeder\CsvSeeder;

class UsersTableSeeder extends CsvSeeder {

    public function run()
    {
        // seed the users table
        $this->seedFromCSV(base_path('path/to/users.csv'), 'users');

        // seed the posts table
        $this->seedFromCSV(base_path('path/to/posts.csv'), 'posts');
    }
}

Usage Examples

Given the following CSV and database table:

// users.csv
first_name,last_name,birth_date,password,favorite_color
Joe,Bar,2000-02-10,joePassword,red
Jim,Foo,1990-02-10,jimPassword,blue
Foo,Bar,1980-02-10,fooPassword,green

// users DB table
id, first_name, last_name, birth_date, password, favorite_color

You can run the seeder with no further setup:

$this->seedFromCSV(base_path('path/to/users.csv'), 'users');

You could even go a step further and omit the table name, as the CSV filename is the same as the table name. CsvSeeder will automatically try to resolve table and column names when they're not defined. If your CSV doesn't have a header row, you'll need to manually define a $mapping, as described in the next section.

Mismatched columns

Unless you have complete control over your CSVs, the headers won't always match up with your DB columns. For example:

// users.csv
first_name, last_name, birth_date, password, favorite_color

// users DB table
id, first_name, last_name, age, password

In this case, you can define $aliases to rename the birth_date column to age before it's inserted:

$this->aliases = [
    'birth_date' => 'age'
];

$this->seedFromCSV(base_path('path/to/users.csv'), 'users');

Alternatively, you can manually define a $mapping for your CSV. A mapping allows you to explicitly choose and rename CSV columns. For example:

// users.csv
first_name, last_name, birth_date, password, favorite_color

// users DB table
id, first_name, last_name, color, password

// users seeder
$this->mapping = [
    0 => 'first_name',
    1 => 'last_name',
    3 => 'password',
    4 => 'color', // renamed from favorite_color
];

$this->seedFromCSV(base_path('path/to/users.csv'), 'users');

When you define a $mapping, a header row on your CSV is not required. In all other cases, CsvSeeder will assume your header row is the first row after $offset_rows.

Insert Callbacks

In some cases, you'll need to manipulate the CSV data directly before it's inserted to the database. Using an $insert_callback, it couldn't be easier! Everytime a $chunk of rows is read from the CSV, it's passed to the default $insert_callback. All you need to do is define your own callback to override it.

Here we'll iterate over individual rows in the chunk and insert them using Model::create():

$this->insert_callback = function ($chunk) {
    foreach($chunk as $row) {
        \App\User::create($row->toArray());
    }
};

$this->seedFromCSV(base_path('path/to/users.csv'), 'users');

Note, $chunk and $row are instances of \Illuminate\Support\Collection so you can easily manipulate and filter the rows and columns:

$this->insert_callback = function ($chunk) {
    foreach($chunk as $row) {
        $user_data = $row->only('first_name', 'last_name', 'password')->toArray();
        \App\User::create($user_data);
    }
};

$this->seedFromCSV(base_path('path/to/users.csv'), 'users');

Configuration

More Examples

CSV with pipe delimited values:

public function __construct()
{
    $this->table = 'users';
    $this->filename = base_path('database/seeds/csvs/your_csv.csv');
    $this->delimiter = '|';
}

Specifying which CSV columns to import:

public function __construct()
{
    $this->table = 'users';
    $this->filename = base_path('database/seeds/csvs/your_csv.csv');
    $this->mapping = [
        0 => 'first_name',
        1 => 'last_name',
        5 => 'age',
    ];
}

Using a model instead of a table:

public function __construct()
{
    $this->model = \App\User::class;
    $this->filename = base_path('database/seeds/csvs/your_csv.csv');
    // optionally, disable the $model_guard to ignore your model's guarded/fillable attributes
    $this->model_guard = false;
}

Skipping the first row of your CSV (Note: If the first row after the offset isn't the header row, a mapping must be defined):

public function __construct()
{
    $this->table = 'users';
    $this->filename = base_path('database/seeds/csvs/your_csv.csv');
    $this->offset_rows = 1;
    $this->mapping = [
        0 => 'first_name',
        1 => 'last_name',
        2 => 'password',
    ];
}

Aliasing a CSV column:

public function __construct()
{
    $this->table = 'users';
    $this->filename = base_path('database/seeds/csvs/your_csv.csv');
    $this->aliases = [
        'age' => 'date_of_birth',
    ];
}

Aliasing a CSV column defined in $mapping:

public function __construct()
{
    $this->table = 'users';
    $this->filename = base_path('database/seeds/csvs/your_csv.csv');
    $this->mapping = [
        0 => 'first_name',
        1 => 'last_name',
        5 => 'birth_date', // in the CSV file, this column is named 'age'
    ];
    $this->aliases = [
        'birth_date' => 'date_of_birth',
    ];
}

Check out the source of Crockett\CsvSeeder\CsvSeeder for more complete information about the available methods.

License

CsvSeeder is open-sourced software licensed under the MIT license


All versions of csv-seeder with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4.0
laravel/framework Version >=5.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package crockett/csv-seeder contains the following files

Loading the files please wait ....