Laravel 5.4 - PHP Artisan Migrate 命令不起作用



我有四个迁移文件,当我在命令行中运行php artisan migrate时,它说:

Nothing to migrate

我也尝试了php artisan migrate --database=vcp

Database [vcp] not configured.

我在.env文件中使用了另一个数据库,并再次运行php artisan migrate命令:

Migration table created successfully.
Nothing to migrate. 

运行

php artisan migrate:refreshphp artisan migrate:resetphp artisan migrate:statusphp artisan migrate --path="database/migrations/migration_file"这些信息

Nothing to rollback.
Nothing to migrate.
No migrations found

composer dump-autoload or composer update

没有帮助。

这是我.env文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=vcp
DB_USERNAME=root
DB_PASSWORD=secret

我的2017_05_10_201750_add_columns_to_users.php

<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class AddColumnsToUsers extends Migration
   {
       /**
         * Run the migrations.
         *
         * @return void
        */
       public function up()
      {
            Schema::table('users', function (Blueprint $table) {
                $table->string('status')->default('online');
                $table->string('api_token');
           });
       }
     /**
      * Reverse the migrations.
      *
      * @return void
     */
    public function down()
     {
         Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('status');
            $table->dropColumn('api_token');
        });
     }
} 

请帮忙!

正如您在上面的评论中已经指出的那样,The changes are done directly on database.那么Nothing to migrate.是正确的。

尝试理解这个概念,迁移包含文件中的表结构,当您运行迁移时,这些表是在数据库中创建的。在迁移文件中进行更改时,必须再次运行迁移,以便将这些更改反映回数据库表。所以流程是:

migration file -> database table

database table -> migration file

以下是对我有用的方法:

首先,删除所有数据库表,包括迁移表。其次,AddColumnsToUsers 文件的更新方法 up(( 如下所示:

public function up()
    {
    DB::beginTransaction();
    try {
        Schema::table('users', function (Blueprint $table) {
            $table->string('status')->default('online');
            $table->string('api_token');
       });
    DB::commit();
    } catch (PDOException $e) {
        DB::rollBack();
        $this->down();
    }
}

这将检查您的数据库以查看表是否已存在,如果不存在,则创建表,如果存在,则回滚。让我知道这是否有效。

有时你像我一样手动编写迁移函数,复制和粘贴旧的函数,然后手动更改文件名。前任:文件 2021_10_13_082805_insert_fields_pages_table.php如果情况相同,请注意类名,它必须包含反映文件名的名称。在此示例中:

class InsertFieldsPagesTable extends Migration

如果忘记更改类名,迁移将无所不操作,也不执行任何操作。

最新更新