This package allows CSV based seeds.
Require this package in your composer.json and run composer update (or run composer require flynsarmy/csv-seeder:1.*
directly):
"flynsarmy/csv-seeder": "1.0.*"
[URL GITHUB](https://github.com/Flynsarmy/laravel-csv-seeder)
Your CSV's header row should match the DB columns you wish to import. IE to import id and name columns, your CSV should look like:
id,name
1,Foo
2,Bar
Seed classes must extend Flynsarmy\CsvSeeder\CsvSeeder
, they must define the destination database table and CSV file path, and finally they must call parent::run()
like so:
use Flynsarmy\CsvSeeder\CsvSeeder;
class StopsTableSeeder extends CsvSeeder {
public function __construct()
{
$this->table = 'your_table';
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
}
public function run()
{
// Recommended when importing larger CSVs
DB::disableQueryLog();
// Uncomment the below to wipe the table clean before populating
DB::table($this->table)->truncate();
parent::run();
}
}
Drop your CSV into /database/seeds/csvs/your_csv.csv or whatever path you specify in your constructor above.
<?php
use Illuminate\Database\Seeder;
use Flynsarmy\CsvSeeder\CsvSeeder;
use Carbon\Carbon;
class ExpedienteTableSeeder extends CsvSeeder
{
public function __construct()
{
$this->table = 'expedientes';
$this->csv_delimiter = '|';
$this->filename = base_path().'/database/seeds/csvs/expedientes-demo.csv';
$this->offset_rows = 1;
$this->mapping = [
0 => 'id',
1 => 'expediente',
2 => 'descripcion',
3 => 'status'
];
$this->should_trim = true;
}
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->command->info('Inicio - Creacion de los expedientes DEMO.');
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
// Se realiza un truncate a la tabla en caso que sea necesario
DB::table($this->table)->truncate();
// Recomendado cuando importo grandes cantidades de datos
DB::disableQueryLog();
parent::run();
//actualizo las fecha de creacion de la base de datos
DB::table($this->table)->update(['updated_at' => Carbon::now(), 'created_at' => Carbon::now()]);
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
$this->command->info('Fin -'.Carbon::now());
}
}
Within the DatabaseSeeder class, you may use the call method to execute additional seed classes. Using the call method allows you to break up your database seeding into multiple files so that no single seeder class becomes overwhelmingly large. Simply pass the name of the seeder class you wish to run:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call(AgenciasTableSeeder::class);
$this->call(CatAduanasTableSeeder::class);
$this->call(CatTipoPagosTableSeeder::class);
$this->call(CatUsertypeTableSeeder::class);
$this->call(EmpresasTableSeeder::class);
$this->call(ExpedienteTableSeeder::class);
$this->call(UsuariosSystemTableSeeder::class);
Model::reguard();
}
}
Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command:
composer dump-autoload
php artisan db:seed
php artisan db:seed --class=UsersTableSeeder
php artisan migrate:refresh --seed