Skip to content

A simple library to load and clean your CSV file into PHP arrays with named keys.

License

Notifications You must be signed in to change notification settings

jtkendall/kurin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kurīn

A simple library to load and clean your CSV file into PHP arrays with named keys. While created specifcally for use with Laravel Database Seeders, it can be used any place you want to convert a CSV file to a PHP array with named keys.

Installation

Kurīn can be installed with composer.

composer require jtkendall/kurin

Usage

Once Kurīn has been added to Laravel you can use it anywhere you want to convert a CSV file to a PHP array.

Basic Example
use jtkendall\Kurin;

$results = Kurin::fromCSV('data/example.csv', ['id', 'name']);
Seeder Example
use Illuminate\Database\Seeder;
use jtkendall\Kurin;

class ExampleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $examples = Kurin::fromCSV(storage_path('app/example.csv'), ['id', 'name', 'slug', 'description']);

        DB::table('examples')->insert($examples);
    }
}

Parameters

Kurīn expects two parameters, the path to the CSV file and the array containing the desired key strings. The keys need to be in the order you want the CSV matched to.

Kurīn however supports a third optional parameter, an array of strings that will be appended to the array as keys after the CSV fields and have their value set to Carbon timestamps.

Carbon Example
use jtkendall\Kurin;

$results = Kurin::fromCSV('data/example.csv', ['id', 'name'], ['created_at', 'updated_at']);

About

A simple library to load and clean your CSV file into PHP arrays with named keys.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages