snowbuilds/laravel-seeder-reset 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

snowbuilds/laravel-seeder-reset

最新稳定版本:0.0.3

Composer 安装命令:

composer require snowbuilds/laravel-seeder-reset

包简介

Reset Laravel seeders before running

README 文档

README

Laravel SeederReset Package Logo

Latest Version on Packagist Total Downloads GitHub Actions

Introduction

Prompt developers to truncate tables and delete old data before executing seeders. Great for seeding projects that prohibit duplicate data!

Laravel SeederReset Package Logo

Installation

You can install the package via composer:

composer require snowbuilds/laravel-seeder-reset

Usage

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 () {
        //
    }
}

List tables to truncate

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 () {
        //
    }
}

Reset Seeders - Call Truncate

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,
        ]);
    }
}

Reset Seeders - Replace Call

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,
        ]);
    }
}

Override Seeder

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,
        ]);
    }
}

Hooks

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,
        ]);
    }
}

Roadmap

  • Truncate tables from list of models
  • Truncate using table names
  • Delete data using queries

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email dev@snowlaboratory.com instead of using the issue tracker.

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固