- Introduction
- Installation
- Usage
- Roadmap
- Changelog
- Contributing
- Security Vulnerabilities
- Code of Conduct
- License
Prompt developers to truncate tables and delete old data before executing seeders. Great for seeding projects that prohibit duplicate data!
You can install the package via composer:
composer require snowbuilds/laravel-seeder-reset
Include the SnowBuilds\SeederReset\Concerns\SeederTruncate
trait in your seeder class. Next time you run the seeder, you will be prompted to truncate seeders. When choose to truncate, the specified models are truncated before running the seeder:
use SnowBuilds\SeederReset\Concerns\SeederTruncate;
use Illuminate\Database\Seeder;
class PostSeeder extends Seeder
{
use SeederTruncate;
public $truncate = [
App\Models\User::class,
App\Models\Comment::class,
App\Models\Post::class,
];
public function run () {
//
}
}
When the truncate property is not enough, you can return an array from the getTruncate
method:
use SnowBuilds\SeederReset\Concerns\SeederTruncate;
use Illuminate\Database\Seeder;
class PostSeeder extends Seeder
{
use SeederTruncate;
public function getTruncate() {
return [
App\Models\User::class,
App\Models\Comment::class,
App\Models\Post::class,
]
};
public function run () {
//
}
}
Sometimes you may have seeders which call other seeders. If you only want to be prompted once you can invoke the truncate method:
use SnowBuilds\SeederReset\Concerns\SeederTruncate;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
use SeederTruncate;
public function run () {
$this->truncate([
UserSeeder::class,
RecipeSeeder::class,
]);
$this->call([
UserSeeder::class,
RecipeSeeder::class,
]);
}
}
If you are looking for something a little more implicit, you can replace the call
method with the reset
method, which will reset each seeder before invoking:
use SnowBuilds\SeederReset\Concerns\SeederTruncate;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
use SeederTruncate;
public function run () {
$this->reset([
UserSeeder::class,
RecipeSeeder::class,
]);
}
}
If you want to keep the same API but want to call other seeders we recommend extending SnowBuilds\SeederReset\Seeder
:
use SnowBuilds\SeederReset\Concerns\SeederTruncate;
use SnowBuilds\SeederReset\Seeder;
class DatabaseSeeder extends Seeder
{
public function run () {
$this->call([
UserSeeder::class,
RecipeSeeder::class,
]);
}
}
Sometimes truncating is not enough, and you need delete specific rows before seeding. We included beforeTruncate
and afterTruncate
hooks which can be used to delete whatever you want. If the hook fails the operation will rollback.
Important The truncate operation is not performed in a transaction due to MySql limitations. If a hook fails, tables that were truncated will remain empty.
use SnowBuilds\SeederReset\Concerns\SeederTruncate;
use SnowBuilds\SeederReset\Seeder;
class DatabaseSeeder extends Seeder
{
public function beforeTruncate()
{
User::moderators()->delete();
User::customers()->delete();
}
public function run () {
$this->call([
UserSeeder::class,
RecipeSeeder::class,
]);
}
}
- Truncate tables from list of models
- Truncate using table names
- Delete data using queries
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email dev@snowlaboratory.com instead of using the issue tracker.
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
The MIT License (MIT). Please see License File for more information.