Skip to content

Instantly share code, notes, and snippets.

@f-giftagram
Forked from andyg1/DatabaseSeeder.php
Created September 6, 2021 23:55
Show Gist options
  • Save f-giftagram/f63aff521c98b30dbe254bd31b400fda to your computer and use it in GitHub Desktop.
Save f-giftagram/f63aff521c98b30dbe254bd31b400fda to your computer and use it in GitHub Desktop.
Truncating Laravel tables before seeding them
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
protected $toTruncate = ['users'];
public function run()
{
Model::unguard();
foreach($this->toTruncate as $table) {
DB::table($table)->truncate();
}
$this->call(UsersTableSeeder::class);
Model::reguard();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment