在上篇文章中我们涉及到这条命令:
php artisan migrate
意思是把项目/database/migrations目录下的所有数据表文件创建进数据库,项目默认自带了两张表:2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resets_table.php
所以会创建这两张表,但是migrate不仅创建了这些表,还会创建一张表:
我门打开这张表:
这张表是记录数据表迁移记录的,这张表是为了数据表操作回滚而存在的。
现在我们执行命令:
php artisan migrate:rollback --step=1
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_100000_create_password_resets_table
命令说明:php artisan migrate:rollback --step=步数
数据库发生的变化:
这时候我们再执行:
php artisan migrate
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
这时候数据库又展现了这样的一幕:
接下来我们输入命令:
php artisan migrate:reset
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolling back: 2014_10_12_000000_create_users_table
Rolled back: 2014_10_12_000000_create_users_table
我们再看数据库:
好了,我们还是让数据表出现吧:
php artisan migrate
现在我们修改一下2014_10_12_000000_create_users_table.php这张表的字段:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username'); //name改成了username
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
我们现在想更新数据表结构,让这个改变的字段更新进数据库的这张表,用php artisan migrate是肯定不行的,给我们返回的结果是Nothing to migrate.
如果想要做到表结构的更新,我们需要用到命令:
php artisan migrate:refresh
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolling back: 2014_10_12_000000_create_users_table
Rolled back: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
这条命令会从数据表内部的数据结构进行检测并达到更新表目的。
这时候我们看下数据库:
这是迁移相关操作,我们继续看看创建migration表的方法。
创建migration表:
php artisan make:migration create_books_table --create=books
Created Migration: 2019_07_12_171543_create_books_table
我们发现项目中多了一个migration表文件,我们看下帮我们生成的文件代码:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
创建表用的是Schema机制,我们添加一些字段:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->string('name'); //添加的字段
$table->string('desc'); //添加的字段
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
执行:
php artisan migrate
刷新数据库的表:
但是在项目上线之后随意这样改动数据表,会造成用户数据丢失等一系列的错误,导致网站不能正常运行。
如果我们想在books表中添加作者字段,我们应该创建一个新的migration文件用于执行添加作者字段:
php artisan make:migration add_author_filed_into_books_table --table=books
Created Migration: 2019_07_12_173128_add_author_filed_into_books_table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddAuthorFiledIntoBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('books', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('books', function (Blueprint $table) {
//
});
}
}
up()为添加,down()为回滚,migration就是用这样的一个机制来进行回滚操作。
仔细看看Schema::create和现在Schema::table的区别,会让你受益匪浅。
现在修改一下代码:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddAuthorFiledIntoBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('books', function (Blueprint $table) {
$table->string('author'); //添加字段
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('books', function (Blueprint $table) {
$table->dropColumn('author'); //删除字段
});
}
}
之后执行:
php artisan migrate
在看数据库中的变化:
到这里,我们大部分migration命令已经介绍过,掌握上述命令,算是对migration命令有一定的熟悉了,还有一些其他的migration命令根据项目开发维护的需要去使用,更多的内容看官方文档就行,尤其是申明字段的方式。
系列文章: