Home

Awesome

<p align="center">Laravel Seedable Migrations</p>

<p align="center"> <a href="https://github.com/Garanaw/seedable-migrations/actions"><img src="https://github.com/Garanaw/seedable-migrations/actions/workflows/tests.yml/badge.svg" alt="Tests"></a> <a href="https://github.com/Garanaw/seedable-migrations/actions"><img src="https://github.com/Garanaw/seedable-migrations/actions/workflows/pint.yml/badge.svg" alt="Linting"></a> </p>

About

Laravel provides a migration system with seeding. However, the seeding system is not very flexible. This package provides a more flexible way to seed your database with seed files linked to migrations.

You can run your seeding after each migration, or at the end of the batch. You can also specify seeder files that should run on the UP method or the DOWN method of your migrations.

Install

To install the package, run the following command:

composer require garanaw/seedable-migrations

After installing the package, you need to publish the migration stubs:

php artisan vendor:publish --provider="Garanaw\SeedableMigrations\SeedableMigrationsServiceProvider"

If you don't want to publish the stubs, you will need to extend your migrations manually from the Garanaw\SeedableMigrations\Migration class.

The new migration file

The migration files will look a bit different now when you run the command php artisan make:migration. The new migration files will look like this:

<?php

declare(strict_types=1);

use Garanaw\SeedableMigrations\Blueprint;
use Garanaw\SeedableMigrations\Enum\SeedAt;
use Garanaw\SeedableMigrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        $this->schema->create(table: $this->getTable(), callback: static function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    public function getTable(): string
    {
        return $this->table;
    }

    public function seedAt(): SeedAt
    {
        return SeedAt::EACH;
    }

    public function down(): void
    {
        $this->schema->dropIfExists($this->getTable());
    }
}

A few things to note here:

The new seeder file

The seeder files will look a bit different now when you run the command php artisan make:seeder. The new seeder files will look like this:

<?php

declare(strict_types=1);

use Garanaw\SeedableMigrations\Seeder;

return MySeeder extends Seeder
{
    public function run(): bool
    {
        // Your seeder code here
    }

    public function getData(): array
    {
        return [];
    }
}

A few things to note here:

How to use

To run your migrations, you can use the php artisan migrate command as usual. The seeding will be done automatically.

To specify the seeder files associated to a migration, you will need to add some methods to the migration file:

    public function seedersUp(): \Illuminate\Support\Collection
    {
        return collect([
            MySeeder::class,
        ]);
    }

    public function seedersDown(): \Illuminate\Support\Collection
    {
        return collect([
            MySeeder::class,
        ]);
    }

The seedersUp() method is used to specify the seeder files that should run on the UP method of the migration. The seedersDown() method is used to specify the seeder files that should run on the DOWN method of the migration. None of them is mandatory. If you don't specify any seeder file, the seeder will not run.

Another optional method is the shouldSeed() method. This method is used to specify if the seeder should run or not. This method is useful if you want to run the seeder only on specific environments. The default implementation is:

    public function shouldSeed(): bool
    {
        return true;
    }

Within the shouldSeed() method, you can use the app()->environment() method to check the environment, or any other logic to determine if the seeder should run or not.

To configure your seeder, you only need to add a configure() method to your seeder class:

    public function configure(
        \Illuminate\Config\Repository $config,
        \Illuminate\Foundation\Application $app,
        \Illuminate\Contracts\Console\Kernel $artisan,
        \Illuminate\Contracts\Debug\ExceptionHandler $exceptionHandler,
        App\Providers\RouteServiceProvider $routeServiceProvider,
        \Psr\Log\LoggerInterface $logger,
    ): void {
        // Your configuration code here. You can inject anything that is configured in your container
    }

Security Vulnerabilities

If you discover a security vulnerability, please create an issue using the issue tracker.

License

The Seedable Migrations library is open-sourced software licensed under the MIT license.