Created
February 4, 2014 16:42
-
-
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.
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
// 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