laravel5.4以上版本的migrate迁移数据库创建表的过程

首先在控制台进行laravel项目的路径下:
PHP artisan migrate:install
将会在数据库中创建migrations的数据表,用来记录迁移的数据表,用来同步

创建数据表:
PHP artisan make:migration create_posts_table
这样将会在项目的database/migratios/ 2018_01_25_025623_create_posts_table.php 的文件

然后在这个文件中添加代码:
<?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.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
注意: 因为laravel5.4 以后的字段长度定义的大于字段长度,采用mb4string 以四个字节算为一个, 所以需要修改
\laravel54\app\Providers\AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//默认采用mb4string 编码, 797/4 = 191
Schema::defaultStringLength(191); //设置默认长度字符串的191
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
最后执行:php artisan migrate ,将会在数据库中创建posts的数据表

猜你喜欢

转载自blog.csdn.net/yhflyl/article/details/79160303
今日推荐