码迷,mamicode.com
首页 > 其他好文 > 详细

laravel5.6 使用迁移创建表

时间:2018-06-11 13:48:06      阅读:665      评论:0      收藏:0      [点我收藏+]

标签:ble   时间   incr   需要   blueprint   sort   facade   TE   ppi   

laravel 使用迁移创建表

创建迁移文件
  • --table 和 --create 选项可以用于指定表名以及该迁移是否要创建一个新的数据表。这些选项只需要简单放在上述迁移命令后面并指定表名:

    php artisan make:migration create_test_users_table --create=test_users
  • 新的迁移位于 database/migrations 目录下,每个迁移文件名都包含时间戳从而允许 Laravel 判断其顺序。
    ```
    <?php

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

class CreateTestUsersTable extends Migration
{
/**
* Run the migrations.

@return void
*/
public function up()
{
Schema::create(‘test_users‘, function (Blueprint $table) {
$table->increments(‘id‘);
$table->char(‘no‘)->nullable()->comment(‘平台用户编号‘)->index(‘no‘);
$table->string(‘name‘)->comment(‘姓名‘);
$table->string(‘email‘)->unique()->comment(‘邮箱‘);
$table->string(‘appid‘)->nullable()->comment(‘绑定微信‘);
$table->string(‘phone‘)->nullable()->comment(‘联系电话‘);
$table->string(‘password‘)->comment(‘密码‘);
$table->string(‘position‘)->nullable()->comment(‘职位‘);
$table->unsignedInteger(‘sort‘)->default(10)->comment(‘排序‘);
$table->text(‘remark‘)->nullable()->comment(‘备注‘);
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
});
}

/**
 * Reverse the migrations.
 * 更新修改
 * @return void
 */
public function down()
{
    Schema::dropIfExists(‘test_users‘);
}

}

- 执行迁移文件

php artisan migrate



##### - 回滚迁移
- 加载依赖

composer require doctrine/dbal

- 创建修改迁移文件 
 

php artisan make:migration update_test_users_table

- 执行过上个迁移文件

php artisan migrate

- 使用迁移回滚操作

注意:rollback 只执行上次执行的迁移文件批次中的down()方法,也就是修改方法
php artisan migrate:rollback

- 操作完成 

Process finished with exit code 0 at 10:24:06.
```

laravel5.6 使用迁移创建表

标签:ble   时间   incr   需要   blueprint   sort   facade   TE   ppi   

原文地址:https://www.cnblogs.com/maomojun/p/9166393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!