Giter VIP home page Giter VIP logo

laravel-db-seed-rollback's Introduction

Laravel db:seed with Rollback option

Introduction
In the context of package development, particularly those involving database seeders, I propose the introduction of the --rollback option for the db:seed command. This feature complements the existing migrate:rollback command and offers a solution for efficient database seeding management.

The Problem
Currently, many projects rely on packages that utilize seeders to alter the database. However, when complications arise, such as errors, changes in requirements, or the need to remove packages, there is a lack of streamlined mechanisms to revert these seeding changes. While the migrate:rollback command helps with migrations, the absence of a corresponding option for seeders presents challenges in complex projects where multiple custom packages are involved.

The Solution
By introducing the --rollback option for the db:seed command, package developers and maintainers can now address these issues.

Use Case
Just run db:seed with the --rollback option and down() function will be invoked!

php artisan db:seed --class="Vendor\\Package\\Database\\Seeders\\UsersSeeder" --rollback 
#or
php artisan db:seed --rollback
namespace Vendor\Package\Database\Seeders

/** If your seeder does not extend the package's Seeder:
 * Sonole\LaravelDbSeedRollback\Illuminate\Database\Seeder
 * then a new temporary file will be created at database/seeders, and it will be deleted after down() execution. 
*/
use Illuminate\Database\Seeder;

class UsersSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
         //\App\Models\User::factory(10)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => '[email protected]',
        // ]);
    }

    /**
     * Reverse the effects of the database seeding operation.
     */
    public function down(): void
    {
        \App\Models\User::where('email', '[email protected]')->delete();
    }
}

laravel-db-seed-rollback's People

Contributors

sonole avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.