学习笔记《Laravel: artisan》--migrate

2016-10-18  本文已影响0人  Julian1009

在Laravel项目目录键入 php artisan list ,会看到artisan的命令列表,其中migrate部分如下

 migrate:generate            Generate a migration from an existing table structure.
 migrate:install             Create the migration repository
 migrate:refresh             Reset and re-run all migrations
 migrate:reset               Rollback all database migrations
 migrate:rollback            Rollback the last database migration
 migrate:status              Show the status of each migration

migrate:generate的功能是根据一个已存在的表,在项目目录database/migrations中产生一个含有已有表结构的php文件。
查看migrate:generate参数,命令行键入php artisan migrate:generate --help,出现以下提示

Usage:
    migrate:generate [options] [--] [<tables>]
Arguments:
    tables        A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments
Options:
  -c, --connection[=CONNECTION]       The database connection to use. [default: "mysql"]
  -t, --tables[=TABLES]               A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments
  -i, --ignore[=IGNORE]               A list of Tables you wish to ignore, separated by a comma: users,posts,comments
  -p, --path[=PATH]                   Where should the file be created?
       --defaultIndexNames             Don't use db index names for migrations
       --defaultFKNames                Don't use db foreign key names for migrations
  -h, --help                          Display this help message
  -q, --quiet                         Do not output any message
  -V, --version                       Display this application version
      --ansi                          Force ANSI output
      --no-ansi                       Disable ANSI output
  -n, --no-interaction                Do not ask any interactive question
      --env[=ENV]                     The environment the command should run under.
  -tp, --templatePath[=TEMPLATEPATH]  The location of the template for this generator
  -v|vv|vvv, --verbose                Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
  Generate a migration from an existing table structure.

举例说明:
命令行键入:php artisan migrate:generate -t user

Using connection: mysql
Generating migrations for: users
Do you want to log these migrations in the migrations table? [Y/n] :

这里我输入n,回车。由于在项目中,Y的效果没有尝试。

Setting up Tables and Index Migrations
Created: myProject/database/migrations/2016_10_18_110946_create_users_table.php
Setting up Foreign Key Migrations
Finished!

此处完成生成migration。

我们可以查看以下生成文件的内容

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
            Schema::create('users', function(Blueprint $table)
            {
                    $table->increments('id');
                    $table->string('name');
                    $table->string('email')->unique();
                    $table->string('password');
                    $table->timestamps();
            });
    }


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

}

这样的脚手架功能可以帮助提高工作的效率。比如,新增数据表字段,并将某些data放入这个新的字段时,可以先快速生成migration,再在其中进行接下来的操作会很方便。

上一篇下一篇

猜你喜欢

热点阅读