laravel5.4使用migration数据库迁移建表


首先要保证laravel的.env文件中的数据库链接已经创建好,在这里我使用的是MYSQL数据库

1 DB_CONNECTION=mysql   //数据库
2 DB_HOST=127.0.0.1  //服务器IP地址
3 DB_PORT=3306    //数据库的端口
4 DB_DATABASE=larvael54  //数据库名
5 DB_USERNAME=root   //数据库用户名
6 DB_PASSWORD=root  //数据库密码

在这里我用的是PHP storm命令行 输入创建migration命令  比如创建users表

PHP artisan make:migration create_users_table

注意:创建migration文件名为create_表名_table,创建成功会在database目录下的migrations目录下创建一个

2017_12_6_205846_create_users_table.php文件,打开文件输入代码
<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {  //这里是你要创建表的字段属性
            $table->increments('id'); 
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

文件创建成功后在PHP storm命令行中输入

PHP artisan migrate

执行完成后,建表成功。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM