Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 457
Alexandre Thebaldi Avatar asked Oct 29 '22 20:10

Alexandre Thebaldi


2 Answers

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.

like image 91
Ohgodwhy Avatar answered Nov 02 '22 20:11

Ohgodwhy


You can't but you can do it with your custom code after do following steps:

  1. Copy file from Illuminate/Database/Console/Seeds/SeederMakeCommand.php to your folder.
  2. Modify getPath() function.
  3. Bind this class to container to override core.
like image 43
May'Habit Avatar answered Nov 02 '22 21:11

May'Habit