laravel 迁移文件
2018年11月5日1.创建迁移文件
1 |
php artisan make create_表名_table --create=表名 |
2.给原有的表添加字段
1 |
php artisan make:migration add_新增字段名_to_users_table --table=users |
3.常用数据结构
$table->increments(‘id’); | id 递增 |
$table->ipAddress(‘visitor’); | ip 地址类型 |
$table->char(‘name’, 4); | 相当于 CHAR 型态,并带有长度。 |
$table->json(‘options’); | json类型 |
$table->macAddress(‘device’); | mac地址类型 |
$table->integer(‘votes’); | int类型 |
$table->float(‘amount’, 8, 2); | 相当于 FLOAT 型态,总共有 8 位数,在小数点后面有 2 位数 |
$table->string(’email’);; | varchar类型变长 |
$table->dateTime(‘created_at’); | 相当于 DATE 型态 |
常用修饰
->comment(‘my comment’) | 增加注释 |
->default($value) | 为此字段指定「默认」值 |
->nullable() | 此字段允许写入 NULL 值 |
->unique() | 此字段唯一 |
4.常用运行迁移命令
1 2 3 4 5 6 7 8 9 |
php artisan migrate #运行所有未运行的迁移 php artisan migrate --force #强制运行会有数据损失 php artisan migrate --seed #在迁移的过程中填充数据 php artisan migrate:reset #回滚迁移 php artisan migrate:refresh --step=5 #指定回迁步数 |