Laravel change migration order

前端 未结 2 1376
自闭症患者
自闭症患者 2020-12-02 15:51

Is there a way how I can change the migrations order without remaking them all? Because now I have a problem with my foreign keys -_- (working with laravel)

2条回答
  •  自闭症患者
    2020-12-02 16:37

    You have to create a custom command that executes php artisan migrate:refresh --path=/database/migrations/name_migration.php repeately with the migrations's name in the order you want.

    Like this:

    1. Create Command class with: php artisan make:command NameClass
    2. Go to app/Console/Commands/ and find the class file NameClass.php
    3. In the NameClass.php you have two attributes $signature (the name of the command) and $description (Information about what your command does).
    4. Set the name and the description of your command.Ex: protected $signature='namecommand'; protected $descripton = 'This method migrate tables in order'
    5. Inside the NameClass.php you have a method called handle(), here you have to declare the code you want to be executed when you write the command.
    6. Register your command. Go to app/Console/Kernel.php and add your class to the list of Command Classes. protected $commands = [ Commands\NameClass::class, ];
    7. Write the command in the terminal. php artisan namecommand

    Example:

    1. php artisan make:command MigrateInOrder

    2. app/Console/Commands/MigrateInOrder.php

    call('migrate:refresh', [
                '--path' => $path ,            
               ]);
            }
        }
    } 
    
    1. Go to app/Console/Kernel.php and register your command
        protected $commands = [
            Commands\MigrateInOrder::class,
        ];
    
    1. Excute the command
       php artisan migrate_in_order
    

提交回复
热议问题