Created
November 14, 2015 15:35
-
-
Save weblee/fe00df1ec23d3f35eb74 to your computer and use it in GitHub Desktop.
Example DatabaseSeeder for Laravel
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\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