1

I want to simplify the following artisan commands because I have several databases and the migrations for each database are stored in a separate folder.

php artisan make:migration {{name}} --path=/database/migrations/{{folder}}

php artisan migrate:rollback  --path=/database/migrations/{{folder}}

to

php artisan make:migration {{name}} {{folder}}

php artisan migrate:rollback {{folder}}

Is this possible and if so how can I implement it?

1
  • 1
    You can write your own commands that call other artisan commands Commented Nov 30, 2022 at 10:01

1 Answer 1

2

Since this is not an option in the Laravel commands, a way to implement this yourself is by writing you own commands that call other artisan commands.

To do so, in your terminal write, for example, php artisan make:command MigrateWithPath to create a new command at app/Console/Commands/MigrateWithPath.php. Then, you can call the vanilla implementation Laravel provides at (vendor\laravel\framework\src) \Illuminate\Database\Console\Migrations\MigrateMakeCommand but then in a way that you specify.

Be sure though that the name of your new command needs to be different from the Laravel one, to prevent recursions. Therefore, I have prefixed the name with app: to be like app:make:migration, but feel free to use something else.

Take a look at the following suggestion:

class MigrateWithPath extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:make:migration {name : The name of the migration}
        {folder? : The location where the migration file should be created}';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->call('make:migration', [
            'name' => $this->argument('name'),
            '--path' => '/database/migrations/' . $this->argument('folder'),
        ]);

        return 0;
    }

Then do the same for the rollback command.

Sign up to request clarification or add additional context in comments.

2 Comments

However, I would like to use the same name as the original command. Do you have an idea of how to accomplish that?
@Elisaveta I suppose you then have to implement the entire code yourself instead of leaning on $this->call('make:migration', ...) because that is the part that initiates the recursion. Unless there is a way to untangle them with some rename or absolute path to keep them apart that I am not aware of. If so, I encourage others to write their answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.