laravel中的数据库迁移

1.创建数据库迁移文件:生成数据库迁移文件,前面跟着时间戳:

php  artisan  make:migration  create_posts_table

  

创建数据库迁移文件:可以重命名数据表名: --table和--create后面跟的都是表名:

php artisan make:migration create_users_table  --table=users

php artisan make:migration create_users_table  --create=users

在make:migration 后面可以跟:--path  跟上创建迁移文件的路径。

 

2.在创建的数据库迁移文件中,编写迁移数据:

<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title',100)->default('');
            $table->text('content');
            $table->integer('user_id')->default(0);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations. reverse:背面;想反  
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

  

3.在app/providers/AppServiceProvider.php中填写,Schema的string默认长度,不然执行migrate命令,会报错。因为string的默认长度是1071,而数据库的最长长度小于他,所以会报错。

public function boot()
    {
        //mb4string
        Schema::defaultStringLength(250);
    }

 

4.执行数据库迁移命令:

php artisan migrate

  

posted @ 2018-03-19 22:48  生如逆旅,一苇以航  阅读(431)  评论(0编辑  收藏  举报