建立一個migrate
php artisan make:migration create_table_anke
編寫migrate建表
Schema::create('anke', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 20);
$table->timestamps();
});
表生成器包含一系列的字段類型用於構建表:
命令 | 描述 |
---|---|
$table->bigIncrements('id'); |
Incrementing ID using a "big integer" equivalent. |
$table->bigInteger('votes'); |
BIGINT equivalent to the table |
$table->binary('data'); |
BLOB equivalent to the table |
$table->boolean('confirmed'); |
BOOLEAN equivalent to the table |
$table->date('created_at'); |
DATE equivalent to the table |
$table->dateTime('created_at'); |
DATETIME equivalent to the table |
$table->decimal('amount', 5, 2); |
DECIMAL equivalent with a precision and scale |
$table->double('column', 15, 8); |
DOUBLE equivalent with precision |
$table->enum('choices', array('foo', 'bar')); |
ENUM equivalent to the table |
$table->float('amount'); |
FLOAT equivalent to the table |
$table->increments('id'); |
Incrementing ID to the table (primary key). |
$table->integer('votes'); |
INTEGER equivalent to the table |
$table->longText('description'); |
LONGTEXT equivalent to the table |
$table->mediumText('description'); |
MEDIUMTEXT equivalent to the table |
$table->morphs('taggable'); |
Adds INTEGER taggable_id and STRING taggable_type |
$table->smallInteger('votes'); |
SMALLINT equivalent to the table |
$table->softDeletes(); |
Adds deleted_at column for soft deletes |
$table->string('email'); |
VARCHAR equivalent column |
$table->string('name', 100); |
VARCHAR equivalent with a length |
$table->text('description'); |
TEXT equivalent to the table |
$table->time('sunrise'); |
TIME equivalent to the table |
$table->timestamp('added_on'); |
TIMESTAMP equivalent to the table |
$table->timestamps(); |
Adds created_at and updated_at columns |
->nullable() |
Designate that the column allows NULL values |
->default($value) |
Declare a default value for a column |
->unsigned() |
Set INTEGER to UNSIGNED |
更多請看https://blog.csdn.net/kjw001122/article/details/79908419
我們在以上運用 $table->timestamps(); 生成添加時間及修改時間,但是這個時間自Laravel 5.3後時間默認爲null ,在操作時需要自己手動添加時間
如果想要在migrate中設置時間爲自動更新 需要進行已下操作
use Illuminate\Support\Facades\DB;
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));