Skip to content

Instantly share code, notes, and snippets.

@jstoone
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstoone/d3d2ed709272522f446e to your computer and use it in GitHub Desktop.
Save jstoone/d3d2ed709272522f446e to your computer and use it in GitHub Desktop.
Basic Laravel 5 Database Seeder
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Tables that needs cleaning
*
* @var array
*/
protected $tables = [
];
/**
* Seeders
*
* @var array
*/
protected $seeders = [
];
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->cleanDatabase();
foreach($this->seeders as $seedClass)
{
$this->call($seedClass);
}
Model::reguard();
}
/**
* Clean the database for previous data
*/
protected function cleanDatabase()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
foreach($this->tables as $table)
{
DB::table($table)->truncate();
}
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment