PHP code example of devlabor / laravel-csv-seeder

1. Go to this page and download the library: Download devlabor/laravel-csv-seeder 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/ */

    

devlabor / laravel-csv-seeder example snippets


use \DevLabor\CsvSeeder\Database\Seeder\CsvSeeder

// ...

class ProductsTableSeeder extends CsvSeeder {
	/**
	 * UsersTableSeeder constructor.
	 */
	public function __construct()
	{
		$this->columnMapping = [
			0 => 'article_no',
			1 => 'name',
			2 => 'text',
			3 => 'price'
		];
	}
	
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		// Recommended when importing larger CSVs
		\Illuminate\Support\Facades\DB::disableQueryLog();
	
		// Uncomment the below to wipe the table clean before populating
		\Illuminate\Support\Facades\DB::table($this->guessTableName())->truncate();
	
		parent::run();
	}
}

// Database table name for seeding. Default guessed by class name.
public $table = '';

// CSV file name for seeding. Default guessed by class name.
public $filename = '';

// Table field that to be hashed, most likely a password field. If your password has a different name, please overload this variable from our seeder class.
public $hashable = 'password';

// An SQL INSERT query will execute every time this number of rows are read from the CSV. Without this, large INSERTS will silently fail.
public $insertChunkSize = 50;

// CSV delimiter
public $csvDelimiter = ';';

// Number of rows to skip at the start of the CSV
public $offsetRows = 0;

// Can be used to tell the import to trim any leading or trailing white space from the column;
public $trimWhitespace = true;

/**
 * The mapping of CSV to table column. If not specified manually, the first row (after $offsetRows) of your CSV will be read as your table columns.
 *
 * In order to read the first, third and fourth columns of your CSV only, use:
 * array(
 *   0 => id,
 *   2 => name,
 *   3 => description,
 * )
 */
public $columnMapping = [];