PHP code example of langleyfoxall / eloquent-csv-importer

1. Go to this page and download the library: Download langleyfoxall/eloquent-csv-importer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

langleyfoxall / eloquent-csv-importer example snippets


...

use LangleyFoxall\EloquentCSVImporter\Traits\CSVMappable;

class Product extends Model
{
    use CSVMappable, HasCSVDefinitions;
    
    ...
    
}

public static function getCSVMappableColumns()
{
    return collect([
        'product_name',
    ]);
}

$definition = (new CSVDefinitionFactory(Product::class))
    ->mapColumns(['product ID' => 'product_name'])
    ->setMeta([
        'name' => '2009 - 2010 products',
        'description' => 'Definitions for the 2009 - 2010 product lists',
    ])->create();

$def = Product::first()->CSVDefinitions()->first;
$products = $def->makeModels($csvFile, []);

$products = $def->makeModels($csvFile, ['product ID']);

$products = $def->makeModels($csvFile, ['product ID', 'price']);

$definition->setDataItemManipulator(function($key, $value, $row) {
    return strtoupper($value);
});

php artisan vendor:publish

php artisan migrate