database migrations 是laravel最强大的功能之一。数据库迁移可以理解为数据库的版本控制器。
php artisan命令行:
php artisan make:migration
ps:该命令行必须在项目的根目录下执行。
如果对该命令行不是很了解,可是添加参数-h,查看说明文档。
Usage:
make:migration [options] [--] Arguments:
name The name of the migration.
Options:--create[=CREATE] The table to be created.--table[=TABLE] The table to migrate.--path[=PATH] The location wherethe migration file should be created.-h, --help Display thishelp message-q, --quiet Do not output any message-V, --version Display thisapplication 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.-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 fordebug
Help:
Create anew migration file
创建数据表
1.直接创建。
php artisan make:migration xxx
这种方式下生成的数据库表类,只有方法,没有初始化数据表的名字已经字段。
classXxx extends Migration
{/**
* Run the migrations.
*
* @return void*/
publicfunction up()
{//}/**
* Reverse the migrations.
*
* @return void*/
publicfunction down()
{//}
}
2.添加参数 --create
php artisan make:migration yyyy --create='yyyy'
这种方式下生成的数据库表类,不仅初始化了数据表的名字,同时添加了默认字段名。
classYyyy extends Migration
{/**
* Run the migrations.
*
* @return void*/
publicfunction up()
{
Schema::create(''yyyy'', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}/**
* Reverse the migrations.
*
* @return void*/
publicfunction down()
{
Schema::drop(''yyyy'');
}
}
3.添加参数 --table
php artisan make:migration yyyyy --table='yyyyy'
这种方式下生成的数据库表类,已经初始化数据表的名字,但是,没有默认添加字段。
classYyyyy extends Migration
{/**
* Run the migrations.
*
* @return void*/
publicfunction up()
{
Schema::table(''yyyyy'', function (Blueprint $table) {//});
}/**
* Reverse the migrations.
*
* @return void*/
publicfunction down()
{
Schema::table(''yyyyy'', function (Blueprint $table) {//});
}
}
不拒打赏:
微信
支付宝