Last active
August 29, 2015 14:17
-
-
Save jstoone/d3d2ed709272522f446e to your computer and use it in GitHub Desktop.
Basic Laravel 5 Database Seeder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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