Skip to content

Instantly share code, notes, and snippets.

@weblee
Created November 14, 2015 15:35
Show Gist options
  • Save weblee/fe00df1ec23d3f35eb74 to your computer and use it in GitHub Desktop.
Save weblee/fe00df1ec23d3f35eb74 to your computer and use it in GitHub Desktop.
Example DatabaseSeeder for Laravel
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
$tables = DB::select('SHOW TABLES');
foreach($tables as $table):
if($table->Tables_in_forairports != 'migrations') {
DB::table($table->Tables_in_makemoney)->truncate();
}
endforeach;
$seeders = [
UserTableSeeder::class, ProductTableSeeder::class,
];
foreach ($seeders as $seeder) {
$this->call($seeder);
}
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
Model::reguard();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment