Search code examples
phplaravellaravel-artisan

Laravel: How to generate seeders in a custom directory


I'm using Laravel 5.5 with laravel-modules v2.

It's easy to generate migrations in a custom directory (inside a module, specifically):

php artisan make:migration create_users_table --path=Modules/User/Database/Migrations

But seems that this isn't possible with seeding classes:

php artisan make:seeder UsersTableSeeder --path=Modules/User/Database/

The "--path" option does not exist.

or passing full relative path:

php artisan make:seeder Modules/User/Database/Migrations/UsersTableSeeder

Creates this exactly folder structure inside ./database/seeds/

or passing full absolute path:

php artisan make:seeder /Modules/User/Database/Migrations/UsersTableSeeder

file_put_contents(\my\file\system\project\database/seeds/C:/Program Files/Git /Modules/User/Database/Seeders/UsersTableSeeder.php): failed to open str eam: No such file or directory

How to generate seeders with artisan command in a custom directory?


Solution

  • You can't. The GeneratorCommand (which the Seeder extends) doesn't care about whether or not folders exist, because it's just going to write the file only.

    /**
     * Get the destination class path.
     *
     * @param  string  $name
     * @return string
     */
    protected function getPath($name)
    {
        return $this->laravel->databasePath().'/seeds/'.$name.'.php';
    }
    

    The only way to achieve what you want is to write your own Seeder command and allow for directory traversal. You can inspect the Illuminate\Database\Console\Migrations\MigrateMakeCommand to see how it's done, it's not very difficult.