```php increments('id'); $table->string('title'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); $table->boolean('enable'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('exams'); } } ``` ``` 4. `title`:測驗的名稱 5. `user_id`:建立測驗的使用者編號 6. `enable`:是否啟用測驗, `boolean()` 在 MySQL 中其實是 `tinyint(1)` 類型,未來我們可能需要做一下型別轉換。 7. 若是要建立 `foreign key ` 必須在還沒有資料的時候就先建立,日後若已經有資料,要再建立 `foreign key ` 就會比較麻煩。(因為必須資料能對應的起來才可以建立) ```php $table->foreign('user_id')->references('id')->on('users'); ``` 8. 建立各種欄位類型可參考: 9. 替欄位加入各種屬性請參考: 10. 要修改欄位方法請參考: 11. 各種索引的建立請參考: 12. 最後執行資料庫遷移即可建出新的資料表 ```bash php artisan migrate ``` 13. 要看資料表是否順利建出可以連到[http://localhost/phpmyadmin/db\_structure.php?server=1&db=homestead](http://localhost/phpmyadmin/db_structure.php?server=1&db=homestead) 14. 若想撤銷剛剛的動作,可執行: ```bash php artisan migrate:rollback ``` 15. 若想刪除全部資料表重來,可以執行 ```bash php artisan migrate:reset ``` [](https://github.com/tad0616/exam56/commit/5a0212512804e19eb54d38dd682b45c0e94cd281)
進階搜尋