Laravel - 資料庫設定 === --- ### 建立資料庫 1. 使用cmd 建DB ```linux mysql -u root ``` => 使用root登入mysql 1. 建立專案用的資料庫 ```linux CREATE DATABASE 資料庫名稱; ``` 1. 建立使用者帳號 ```linux CREATE USER 使用者帳號@localhost IDENTIFIED BY '使用者密碼'; ``` 1. 依照需要設定使用者權限 ```linux GRANT ALL PRIVILEGES ON laravel.* TO 使用者帳號@localhost; ``` 1. 依照需求編輯專案目錄中的 .env 設定檔,填入 MySQL/MariaDB 資料庫的設定: ```linux DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=資料庫名稱 DB_USERNAME=使用者帳號 DB_PASSWORD=使用者密碼 ``` 1. 重新php artisan serve 連線 (因為.env變動了) * 以上MySQL 部分,需遠端連結或開其他權限,參考以下連結: https://www.mysql.tw/2018/05/mysql.html https://blog.xuite.net/towns/hc/65849335 --- ### 資料庫連結 1. 在 *.env* 設定環境 1. 在 *config/database.php* 找到 connnections 進行連結 --- ### 建立資料表 #### 建立資料庫結構(資料表) 範例為建 users 資料表(命名慣例使用 snake_case) 1. 使用migration 建立資料表 ```linux php artisan make:migration create_users_table ``` 也可以使用migration 建立資料表,同時建立model ```linux php artisan make:model User --migration ``` * make:migration 資料表是user集合,用複數 * make:model Model是指每一列資料,用單數 1. 在migration 的function up(){ }設定users 資料結構(欄位內容) ```php Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); ``` * public function up(){ 這裡設定執行migrate 要做的事 新建這類 } * public function down(){ 這裡設定執行migrate:rollback 要做的事 刪除這類 } * 欄位變數型態或額外屬性設定 參考官網: https://laravel.com/docs/6.x/migrations#columns 1. 執行migrate 建立資料庫 ```linux php artisan migrate ``` #### 復原資料庫結構 * rollback 回到上一步 或指定退幾步 ```linux php artisan migrate:rollback php artisan migrate:rollback --step=5 ``` * fresh 清空資料庫後再執行每個migration 所以會有乾淨的資料表 ```linux php artisan migrate:fresh // Refresh the database and run all database seeds... php artisan migrate:refresh --seed // 清空後 再執行最後5步的migration php artisan migrate:refresh --step=5 ``` * reset 清空資料庫 所以只有一個空的資料庫 ```linux php artisan migrate:reset ``` #### 其他 migration 動作指令 * rollback、reset、refresh... 直接看[官網](https://laravel.com/docs/6.x/migrations#running-migrations) --- ### 修改資料表 #### 修改資料表名 * 建立一個migration 在up() 寫入以下內容 然後執行migrate (down() 就把$from $to 對調就好) ```php Schema::rename($from,$to); ``` #### 欄位異動 * 前置動作 在修改前,需要先在*composer.json* 加入doctrine/dbal 的相依性 (官網寫的;有時間測試一下沒執行這個動作會怎樣...) ```linux composer require doctrine/dbal ``` * 寫異動欄位的migration 用 動作_to_表名 就會在migration 自動代入table的部分 ```linux php artisan make:migration add_gender_to_uers // 我覺得以下寫法雖然比較長 但更清楚這個migration 的目的 php artisan make:migration add_column_gender_to_uers_table ``` 或是在指令中 指定table (但這樣比較不清楚這個migration 目的) ```linux php artisan make:migration add_column_users --table=users ``` 檢查 add_column_us_to_us_table 這個migration 就會看到自動代入Schema::table()的部分 如果migration 名稱用很free 的寫法,那就要自己寫這段 ```php class AddColumnGenderToUersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // 有指定資料表就會帶出這3行 Schema::table('uers', function (Blueprint $table) { // }); } /** * Reverse the migrations. * * @return void */ public function down() { // 有指定資料表就會帶出這3行 Schema::table('uers', function (Blueprint $table) { // }); } } ``` * 新增欄位 (就上面的範例) ```php public function up() { Schema::table('users', function (Blueprint $table) { $table->string('gender'); }); } public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('gender'); }); } ``` * 修改欄位屬性 在新指定的屬性後面加->change() ```php Schema::table('users', function (Blueprint $table) { $table->string('name', 50)->change(); }); Schema::table('users', function (Blueprint $table) { $table->string('name', 50)->nullable()->change(); }); ``` * 重新命名欄位 (那個相依性套件就是用在這裡) ```php Schema::table('users', function (Blueprint $table) { $table->renameColumn('from', 'to'); }); ``` * 刪除欄位 ```php Schema::table('users', function (Blueprint $table) { $table->dropColumn('votes'); }); // 同時刪除多筆欄位 Schema::table('users', function (Blueprint $table) { $table->dropColumn(['votes', 'avatar', 'location']); }); ``` --- * 補充:出現錯誤代碼 **SQLSTATE[HY000] [1049]** 若*config/database.php* 、*.env* 都沒問題,那可能是需要清cache ```linux D:\cat_shop_laravel>php artisan config:cache Configuration cache cleared! Configuration cached successfully! ```