Home  >  Article  >  PHP Framework  >  Avoid the pit! A pitfall of Laravel database migration function

Avoid the pit! A pitfall of Laravel database migration function

藏色散人
藏色散人forward
2021-06-11 11:51:402062browse

The following tutorial column will introduce you to a pitfall of Laravel's database migration function. I hope it will be helpful to friends who need it! Remember the pitfalls of Laravel database migration function

Laravel 8
  • Required Laravel Database migration function, because I am a Virgo with obsessive-compulsive disorder, so I like to name the files myself. For example:

But when executing the migration error: Avoid the pit! A pitfall of Laravel database migration function

After several rounds of attempts, I found that Avoid the pit! A pitfall of Laravel database migration functionLaravel

    File name naming format for migration files
  • has a fixed parsing, so when I write the file name exactly according to its style, it will run normally

Of course the class name It must also be unified: Avoid the pit! A pitfall of Laravel database migration function

The point of complaint is: Avoid the pit! A pitfall of Laravel database migration functionThe document does not mention the mandatory requirements for file name naming style

Therefore Recording this pitfall today
  • seems to be another pitfall or a shortcoming of the framework: that is, the table structure cannot be continuously integrated . For example, today I need to add fields to a certain table. But it has been migrated at this time, then
  • There is no way to run the same migration file again and a new migration file must be created
  • . Of course, I also understand that this is to facilitate rollback, but my requirement is Continuously adding fields for new functions does not require rollback, so my solution is to clear migrations this table every time, because this table controls whether it can be repeated Where the same migration file is run, the code is as follows:
    trait Trait_Migrate
    {
        /**
         * @desc 数据表:初始化
         * @return Void
         */
        static public function CI_Table_Structure() : Void
        {
            # 清空记录表以保证能持续迁移
            DB::table( 'migrations' ) -> truncate();
            # 执行迁移
            Artisan::call( 'migrate --force --path=/database/migrations/' . SELF::$CI_File );
        }
    
    }
    Generally speaking, it will be smooth sailing if you follow the rules of the framework document completely. However, when doing projects, the default functions of the framework cannot meet the needs. The situation is very normal
  • Related recommendations:
The latest five Laravel video tutorials

The above is the detailed content of Avoid the pit! A pitfall of Laravel database migration function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete