欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用laravel的migrate創(chuàng)建數(shù)據(jù)表的方法

 更新時間:2019年09月30日 11:48:24   作者:healer-c  
今天小編就為大家分享一篇使用laravel的migrate創(chuàng)建數(shù)據(jù)表的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

laravel中可以使用migration創(chuàng)建數(shù)據(jù)表,這使得數(shù)據(jù)庫的遷移非常便利,下面介紹一下laravel中使用migration創(chuàng)建數(shù)據(jù)表的過程。數(shù)據(jù)庫使用的是mysql,laravel版本為5.5

1. 創(chuàng)建并連接數(shù)據(jù)庫

創(chuàng)建數(shù)據(jù)庫

在命令行中輸入mysql -u root -p然后輸入數(shù)據(jù)庫密碼,

創(chuàng)建數(shù)據(jù)庫create database work_space,

回車完成數(shù)據(jù)庫的創(chuàng)建

連接數(shù)據(jù)庫

打開項目中的.env文件

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:kFEhG73pi95EeRVeveIfo11Q0bSui/4Y2tKvjiT0zFc=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1  
DB_PORT=3306
DB_DATABASE=work_space //數(shù)據(jù)庫名 
DB_USERNAME=root  //用戶名
DB_PASSWORD=root  //密碼

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

2. 使用migration創(chuàng)建數(shù)據(jù)表

創(chuàng)建一個migration

打開項目根目錄(我的是/var/www/html/work_space/)

輸入命令:php artisan make:migration create_table_users

如上則成功創(chuàng)建一個migration,

在database/migrations/ 會發(fā)現(xiàn)多了一個名為

2018_07_31_143907_create_table_users.php

打開這個文件,并在up方法中添加要建的表中的字段信息,如下:

<?php

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

class CreateTableUsers extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    // 創(chuàng)建用戶表
    Schema::create('users', function (Blueprint $table) {
      $table->increments('user_id');
      $table->string('user_email',32)->default('')->comment('用戶登錄名:企業(yè)郵箱');
      $table->string('user_password',32)->default('')->comment('用戶密碼,初始值為企業(yè)郵箱');
      $table->ipAddress('user_ip')->default('')->comment('用戶最后一次登錄ip');
      $table->integer('user_login_cnt')->default(0)->comment('用戶登錄次數(shù)');
      $table->timestamps();
    });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    //
  }
}

在命令行中執(zhí)行php artisan migrate,結(jié)果如下(我創(chuàng)建了四張表):

打開數(shù)據(jù)庫,查看有哪些表,show tables結(jié)果如下:

以上便完成了使用migration創(chuàng)建數(shù)據(jù)表,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論