Project

kurin

0.0
No release in over 3 years
Low commit activity in last 3 years
A Rails gem that presents routes in a clean way.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

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']);