實作資料庫
昨天有規劃一下資料表欄位,所以今天就來實作它吧!還記得在產生 Model 的時候有新增了一個 Migration ,這個東西可能一開始不太知道是什麼,它是一個可以把建立資料表這件事交由程式碼的檔案,迅速的創建好需要的資料表。如果是多人共同開發的時候,可以方便拿到最新版的資料庫。
/animal/database/migrations/2019_08_22_201730_create_animals_table.php
檔案已經包含 up
、 down
兩個方法,表示跑這個檔案的時候,會執行 up 寫好的內容,如果恢復資料庫時會跑 down 的方法
class CreateAnimalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('animals', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('type_id')->comment('動物分類');
$table->string('name')->comment('動物的暱稱');
$table->date('birthday')->nullable()->comment('生日');
$table->string('area')->nullable()->comment('所在地區');
$table->boolean('fix')->default(false)->comment('結紮情形');
$table->text('description')->nullable()->comment('簡單敘述');
$table->text('personality')->nullable()->comment('動物個性');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('animals');
}
}
閱讀更多