Skip to content

Instantly share code, notes, and snippets.

@kostaspt
Created February 4, 2014 16:42
Show Gist options
  • Save kostaspt/8807423 to your computer and use it in GitHub Desktop.
Save kostaspt/8807423 to your computer and use it in GitHub Desktop.
For Laravel database seeder. It truncates all tables and then it runs all seeders available.
// File: app/database/seeds/DatabaseSeeder.php
class DatabaseSeeder extends Seeder {
public function run()
{
if (App::environment() === 'production') exit();
Eloquent::unguard();
// Truncate all tables, except migrations
$tables = DB::select('SHOW TABLES');
foreach ($tables as $table) {
if ($table->Tables_in_database !== 'migrations')
DB::table($table->Tables_in_database)->truncate();
}
// Find and run all seeders
$classes = require base_path().'/vendor/composer/autoload_classmap.php';
foreach ($classes as $class) {
if (strpos($class, 'TableSeeder') !== false)
{
$seederClass = substr(last(explode('/', $class)), 0, -4);
$this->call($seederClass);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment