首先在控制台进行laravel项目的路径下:标签:function,use,Illuminate,migrate,数据库,posts,laravel5.4,table,Schema From: https://blog.51cto.com/luckyqilin/5952238
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的数据表