首页 > 数据库技术 > 详细

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

时间:2017-12-06 19:32:04      阅读:274      评论:0      收藏:0      [点我收藏+]

首先要保证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

执行完成后,建表成功。

 

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

原文:http://www.cnblogs.com/ywhzmr/p/7994166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!