【问题标题】:How to create two tables migration using php artisan command for Laravel Project?如何使用 php artisan 命令为 Laravel 项目创建两个表迁移?
【发布时间】:2015-02-24 04:41:37
【问题描述】:

我正在使用 composer 来迁移两个这样的表:

php artisan make:migration create_two_tables --create="projects","tasks"

它在database->migration 文件夹中创建文件。

但在迁移时使用

php artisan migrate

它像这样在数据库中创建唯一的表:

'projects,tasks'

作为一张桌子。

我只想要一个文件和一个命令,就像我在顶部所做的那样,用于迁移 db 中的两个表。

有没有可能得到这个?

注意:我的上级命令我不要不惜一切代价更改数据库迁移文件...

谁能帮帮我...

【问题讨论】:

    标签: php composer-php laravel-5


    【解决方案1】:

    make:migration 命令只是从模板创建一个新的迁移文件。要实际定义迁移,您通常必须编辑该文件。所以在你的情况下,你会这样做:

    php artisan make:migration create_two_tables --create="projects"
    

    然后打开***_create_two_tables.php迁移文件,添加第二个表:

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function(Blueprint $table)
        {
    
        });
    
        Schema::create('tasks', function(Blueprint $table)
        {
    
        });
    }
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('projects');
        Schema::drop('tasks');
    }
    

    通常您还想在表中添加实际列。而你在 Schema::create 闭包内做的事情:

    Schema::create('projects', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        // and so on
    });
    

    Read more about creating tables with the Schema Builder here

    【讨论】:

    • 我已经这样做了,我想通过命令提示符创建这个表,有没有这样的可能性
    • 没有你自己的命令。但是打开文件复制粘贴+改表名就这么难吗?
    【解决方案2】:

    如果您只想通过 artisan 创建两个表而不向这些表添加任何列,您可以创建两个迁移。为每个表创建一个。当你运行

     php artisan make:migration create_projects_table --create=projects    
     php artisan make:migration create_tasks_table --create=tasks
     php artisan migrate 
    

    它将执行两个迁移并为您创建两个表。

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 1970-01-01
      • 2015-03-28
      • 2020-10-16
      • 2014-04-01
      • 1970-01-01
      • 2017-09-27
      • 2021-06-15
      • 2019-04-02
      相关资源
      最近更新 更多