Laravel 6.X 數據庫遷移 建立表 與 修改表

數據庫遷移建立表

本篇文章中使用的是mysql數據庫,其餘數據庫須要修改env文件和app配置,請其餘地方搜索一下就會找到。php


建立示例

1.建立users表:mysql

命令行鍵入sql

  1 php artisan make:migration create_users_table //users複數

數據庫

  1 php artisan make:migration create_users_table --create=users//--create及對應的表名 users 

執行命令後,會在 /database/migrations/ 文件夾下生成對應的數據庫遷移文件,經過修改文件裏的up 方法和 down方法;json

  • 當咱們運行遷移時,up 方法會被調用。
  • 當咱們回滾遷移時,down 方法會被調用。
  1   public function up()
  2     {
  3         Schema::create('users', function (Blueprint $table) {
  4             $table->bigIncrements('id');
  5             $table->string('name')->unique();
  6             $table->string('email')->unique();
  7             $table->timestamp('email_verified_at')->nullable();
  8             $table->string('password');
  9             $table->rememberToken();
 10             $table->timestamps();
 11         });
 12     }

2.建立一個數據模型的時候 須要同時建立對應遷移文件能夠加上 –m:app

  1 php artisan make:model Models/Moment -m 

就在Models文件夾下建立了Moment數據模型 -m在 database/migrations建立了對應的數據庫遷移文件。【注:相似的還有-f【工廠類】 –c【控制器】,能夠合用-cfm,-cm等】composer

保存後執行post

  1 php artisan migrate


會建立對應up方法中配置的字段的數據表。ui

【上面up方法中建立一個名爲users的數據表,表包含id,name,email,email_verified_at,password,rememberToken[注:記住我選項被選中時生成],created_at,updated_at字段】spa


數據庫遷移建立表


從此若想修改完善以前的已經建立好的數據表 不能再操做以前的數據庫遷移文件。

修改 新增或刪除字段須要建立一個新的數據庫遷移文件:

操做前

請檢查你的composer.json文件中已經添加好了doctrine/dbal依賴,Doctrine DBAL庫是用來肯定數據表中字段當前狀態,及用於實現目標調整的SQL查詢語句的建立工做的,安裝依賴請在控制檯中執行:

  1 composer require doctrine/dbal

安裝完成後,接下來:

新增字段

在控制檯中執行命令:

php artisan make:migration add_要添加的字段名_to_要添加字段的表名_table --table=要添加字段的表名

migrate的文件名稱 上面那樣寫只是爲了儘可能規範而已,重要的是遷移文件裏面的內容:

爲示例向users表中添加一個post_id字段,因此咱們的命令以下:

  1 php artisan make:migration add_post_id_to_users_table --table=users

而後會在database/migrations建立了對應的數據庫遷移文件***_add_post_id_to_users_table.php:

  1 public function up()
  2     {
  3         Schema::table('users', function (Blueprint $table) {
  4             $table->bigInteger('post_id')->nullable()->after('email');
  5         });
  6     }
  7 
  8     public function down()
  9     {
 10         Schema::table('users', function (Blueprint $table) {
 11             $table->dropColumn('post_id');
 12         });
 13     }

【注意方法裏已經不是Schema::create方法的調用,而是Schema::table方法的調用了。】

而後,運行

  1  php artisan migrate
便可生效

修改原有字段屬性

若是是修改,就調用change方法便可,好比想增長name 字段的字符串長度:

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->string('name', 50)->change();
  3 });

好比將字段修改成可空:

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->string('name', 50)->nullable()->change();
  3 });
而後,運行
  1  php artisan migrate
便可生效


修改字段名

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->renameColumn('from', 'to');//from字段更名爲to字段
  3 });
而後,運行
  1  php artisan migrate
便可生效

刪除字段

若是目的是刪除以前已經存在的表的某些字段,新建數據庫遷移文件後,在該文件的up方法中鍵入以下代碼;

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->dropColumn('votes');//刪除users表的votes字段
  3 });
  4 
  5 Schema::table('users', function (Blueprint $table) {
  6     $table->dropColumn(['votes', 'avatar', 'location']);//刪除users表裏面的votes,avatar,location這三個字段
  7 });
而後,運行
  1  php artisan migrate
便可生效
相關文章
相關標籤/搜索