laravel 5.4 中使用migrate

1. 创建表结构

a.

命令: php artisan make:migration create_posts_table

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', 120)->default("");
            $table->text('content');
            $table->integer('user_id')->default(0);
            $table->timestamps();
        });
    }

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

3. 当自定义完表属性后,生成表

命令: php artisan migrate

注意:如果是laravel5.4 会报错

解决方案:

a.修改文件:app\Providers\AppServiceProvider.php

扫描二维码关注公众号,回复: 3915794 查看本文章

b.   use Illuminate\Support\Facades\Schema;

c.      Schema::defaultStringcLength(191);

猜你喜欢

转载自www.cnblogs.com/wuheng1991/p/9901513.html