PHP Classes

Flexible Laravel Migration Seeder: Provide a Laravel migration data seeder class

Recommend this page to a friend!
  Info   View files Documentation   View files View files (34)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-12-29 (6 months ago) RSS 2.0 feedNot enough user ratingsTotal: 14 All time: 11,315 This week: 488Up
Version License PHP version Categories
seedable-migrations 1.0MIT/X Consortium ...7Databases, Libraries, Design Patterns, P...
Description 

Author

This package can provide a Laravel migration data seeder class.

It implements a seedable migrations service that registers listener classes to handle migration end events.

These event listener classes are called database migration seeder classes that can implement a simpler migration data seeder.

Applications can extend the base seeder class this package provides to implement simple database migration data seeding.

Innovation Award
PHP Programming Innovation award nominee
January 2024
Number 4
Laravel is a popular PHP framework that many developers use to implement PHP applications.

When an application needs to evolve, it often needs to upgrade the database schema.

Laravel provides classes to implement the migration and seeding of the application database schema.

Since many applications need an initial seeding, whether for configuration or base data, this package implements a Laravel database migration service that can simplify the migrations by allowing developers to configure the mentioned seeding more flexibly and configurable.

Manuel Lemos
Picture of Alun
  Performance   Level  
Name: Alun is available for providing paid consulting. Contact Alun .
Classes: 2 packages by
Country: Spain Spain
Age: 35
All time rank: 4522121 in Spain Spain
Week rank: 91 Up4 in Spain Spain Up
Innovation award
Innovation award
Nominee: 1x

Documentation

<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 schema now is a property of the migration class. If you prefer the facade, you can still use it with `Schema::create()`.
  • The `up()` and `down()` now have a return typehint of `void`. This is not mandatory, but it is recommended.
  • Two new methods are added: `getTable()` and `seedAt()`. The `getTable()` method is used to get the table name of the migration.
  • The `seedAt()` method is used to specify when the seeder should run. You can return `SeedAt::EACH` to run the seeder after each migration, or `SeedAt::BATCH` to run the seeder at the end of the batch. If you don't want to run the seeder, you can return `SeedAt::NONE`.

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:

  • The `run()` method is now mandatory. This is where you should put your seeder code.
  • The `getData()` method is used to return the data that should be seeded. This method is mandatory now as it follows an interface. If you don't want to seed anything, you can return an empty array.
  • The `run()` method now has a return typehint of `bool`. This is mandatory, as it follows an interface, and it's used to store the failed seeders.
  • If your seeder contains a method called `configure()`, it will be called through the `Container::call()`. This means that you can use dependency injection in your seeder class to add extra configuration if needed.
  • Alternatively, you can create a `__construct()` method in your seeder class. This method will be called before the `run()` method, and you can use dependency injection in it, but it will require the parameters from the parent class.

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.


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (4 files, 5 directories)
Files folder imagetests (2 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file pint.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (2 files)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file pint.yml Data Auxiliary data
  Accessible without login Plain text file tests.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageCache (1 file)
Files folder imageContracts (1 file)
Files folder imageEnum (1 file)
Files folder imageListeners (3 files)
Files folder imagestubs (4 files)
  Plain text file Blueprint.php Class Class source
  Plain text file Migration.php Class Class source
  Plain text file SeedableMigrationsServiceProvider.php Class Class source
  Plain text file Seeder.php Class Class source

  Files folder image Files  /  src  /  Cache  
File Role Description
  Plain text file SeedableMigrations.php Class Class source

  Files folder image Files  /  src  /  Contracts  
File Role Description
  Plain text file SeedableMigration.php Class Class source

  Files folder image Files  /  src  /  Enum  
File Role Description
  Accessible without login Plain text file SeedAt.php Aux. Auxiliary script

  Files folder image Files  /  src  /  Listeners  
File Role Description
  Plain text file MigrationEndedListener.php Class Class source
  Plain text file MigrationListener.php Class Class source
  Plain text file MigrationsEndedListener.php Class Class source

  Files folder image Files  /  src  /  stubs  
File Role Description
  Plain text file migration.create.stub Class Class source
  Plain text file migration.stub Class Class source
  Plain text file migration.update.stub Class Class source
  Plain text file seeder.stub Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageFixtures (8 files)
Files folder imageUnit (1 file, 2 directories)
  Plain text file Pest.php Class Class source
  Plain text file TestCase.php Class Class source

  Files folder image Files  /  tests  /  Fixtures  
File Role Description
  Plain text file ConfigurableSeeder.php Class Class source
  Plain text file DummyMigration.php Class Class source
  Plain text file DummySeeder.php Class Class source
  Plain text file EndOfBatchMigration.php Class Class source
  Plain text file MigrationWithDownSeeders.php Class Class source
  Plain text file MigrationWithUpSeeders.php Class Class source
  Plain text file NeverMigration.php Class Class source
  Plain text file SeedEachMigration.php Class Class source

  Files folder image Files  /  tests  /  Unit  
File Role Description
Files folder imageCache (1 file)
Files folder imageListeners (2 files)
  Plain text file MigrationTest.php Class Class source

  Files folder image Files  /  tests  /  Unit  /  Cache  
File Role Description
  Plain text file SeedableMigrationsTest.php Class Class source

  Files folder image Files  /  tests  /  Unit  /  Listeners  
File Role Description
  Plain text file MigrationEndedListenerTest.php Class Class source
  Plain text file MigrationsEndedListenerTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:14
This week:0
All time:11,315
This week:488Up