如何手动命名Laravel迁移,以便“ php artisan migration”可以运行它?

时间:2019-12-04 14:52:13

标签: php laravel

我有一个用PHP编写的附带项目,可以根据我的审核表中的信息自动生成迁移文件。由于该文件是由我的应用程序创建的,而没有使用artisan命令,因此我不知道如何使Laravel看到我的迁移文件。

我的应用程序生成的迁移文件的示例:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class sampleMigration extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('audit_database_index_column_relations', function(Blueprint $table){
        $table->integer('relation_id');
        $table->integer('database_index_id');
        $table->integer('database_column_id');
        $table->integer('sequence_position');
        $table->integer('is_deleted');
});
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('audit_database_index_column_relations');
    }
}

关于我的尝试,我首先尝试指定链接到我的迁移文件的路径。但是,它显示的错误是

Symfony\Component\Debug\Exception\FatalThrowableError  : Class '' not found

  at C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illumi
nate\Database\Migrations\Migrator.php:448
    444|     public function resolve($file)
    445|     {
    446|         $class = Str::studly(implode('_', array_slice(explode('_', $file), 4)
));
    447|
  > 448|         return new $class;
    449|     }
    450|
    451|     /**
    452|      * Get all of the migration files in a given path.

  Exception trace:

  1   Illuminate\Database\Migrations\Migrator::resolve("sampleMigration")
      C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illum
inate\Database\Migrations\Migrator.php:186

  2   Illuminate\Database\Migrations\Migrator::runUp("C:\Users\L0tusw0w\Desktop\Larave
l\testProject/database/migrations/sampleMigration.php")
      C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illum
inate\Database\Migrations\Migrator.php:162

在我的第二次尝试中,我以up()函数作为示例替换了默认迁移“ create_user_table”的up()函数,并且正如我期望的那样,它执行起来没有任何问题!通常,我认为该错误一定是由于我对文件的命名方式造成的。

因此,我想了解如何命名我的迁移文件,以便Laravel可以检测并运行它们

先谢谢您!

1 个答案:

答案 0 :(得分:5)

在尝试运行迁移时收到的错误消息实际上提供了在这种情况下所需的答案。该函数使用文件名来确定迁移类的名称。我们从最里面的括号中读取了这样的链接函数,因此从头开始

explode('_', $file)

该方法获取文件名并将其分解为一个数组,在分数下分割。所以

  

2014_10_12_100000_create_password_resets_table

成为

  

[       “ 2014”,       “ 10”,       “ 12”,       “ 100000”,       “创建”,       “密码”,       “重置”,       “表”,   ]

接下来,该方法将数组的前4个元素切开。这些元素包含创建迁移的时间戳,以确定运行迁移的顺序,但与类名无关。

array_slice(explode('_', $file), 4)

现在我们有了

  

[       “创建”,       “密码”,       “重置”,       “表”,   ]

接下来,数组将折叠回字符串,并用下划线将每个单词分隔

implode('_', array_slice(explode('_', $file), 4))

给予我们

  

create_password_resets_table

最后,Laravel Str :: studly helper函数将此新字符串转换为studly大小写,以确定类名

Str::studly(implode('_', array_slice(explode('_', $file), 4)));
  

CreatePasswordResetsTable

因此,要创建迁移文件名,请以创建文件时的时间戳记开头,然后以小写形式添加类名,并用下划线分隔。

Class SampleMigration extends Migration

将会

  

2019_12_04_37860000_sample_migration

特别注意:不要跳过时间戳或尝试在前4个元素中抛出所需内容。文件在目录中出现的顺序就是执行迁移的顺序,时间戳记确保始终以正确的顺序运行迁移。跳过此步骤可能会导致更改表的迁移在创建表之前运行,或者更改顺序混乱,从而给您带来当前表意外的结果。