本文介绍了Laravel 的数据库迁移的方法,分享给大家,具体如下:
生成迁移
--table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时会创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表:
php artisan make:migration create_users_table php artisan make:migration create_users_table --create=users php artisan make:migration add_votes_to_users_table --table=users
添加字段
\database\migrations\2017_07_30_133748_create_users_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* 运行数据库迁移
*
* @return void
*/
public function up()
{
//
Schema::create('users',function (Blueprint $table){
$table->increments('id')->comment('递增ID');
$table->string('email',60)->comment('会员Email');
$table->string('phone',20)->comment('会员手机号');
$table->string('username',60)->comment('用户名');
$table->string('password',32)->comment('用户密码');
$table->char('rank',10)->comment('会员等级');
$table->unsignedSmallInteger('sex')->comment('性别;0保密;1男;2女');
$table->unsignedSmallInteger('status')->comment('用户状态');
$table->ipAddress('last_ip')->default('0.0.0.0')->comment('最后一次登录IP');
$table->timeTz('last_login')->comment('最后一次登录时间');
$table->timestamps();
});
}
/**
* 回滚数据库迁移
*
* @return void
*/
public function down()
{
//
Schema::drop('users');
}
}
要创建一张新的数据表,可以使用 Schema facade 的 create 方法。create 方法接收两个参数:第一个参数为数据表的名称,第二个参数为一个 闭包 ,此闭包会接收一个用于定义新数据表的 Blueprint 对象
你可以方便地使用 hasTable 和 hasColumn 方法来检查数据表或字段是否存在:
if (Schema::hasTable('users')) {
//
}
if (Schema::hasColumn('users', 'email')) {
//
}
如果你想要在一个非默认的数据库连接中进行数据库结构操作,可以使用 connection 方法:
Schema::connection('foo')->create('users', function (Blueprint $table) {
$table->increments('id');
});
你可以在数据库结构构造器上设置 engine 属性来设置数据表的存储引擎:
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
});
重命名与删除数据表
Schema::rename($from, $to);//重命名
//删除已存在的数据表
Schema::drop('users');
Schema::dropIfExists('users');
创建字段
Schema::table('users', function (Blueprint $table) {
$table->string('email');
});
| 命令 | 描述 |
|---|---|
| $table->bigIncrements('id'); | 递增 ID(主键),相当于「UNSIGNED BIG INTEGER」型态。 |
| $table->bigInteger('votes'); | 相当于 BIGINT 型态。 |
| $table->binary('data'); | 相当于 BLOB 型态。 |
| $table->boolean('confirmed'); | 相当于 BOOLEAN 型态。 |
| $table->char('name', 4); | 相当于 CHAR 型态,并带有长度。 |
| $table->date('created_at'); | 相当于 DATE 型态 |
| $table->dateTime('created_at'); | 相当于 DATETIME 型态。 |
| $table->dateTimeTz('created_at'); | DATETIME (带时区) 形态 |
| $table->decimal('amount', 5, 2); | 相当于 DECIMAL 型态,并带有精度与基数。 |
| $table->double('column', 15, 8); | 相当于 DOUBLE 型态,总共有 15 位数,在小数点后面有 8 位数。 |
| $table->enum('choices', ['foo', 'bar']); | 相当于 ENUM 型态。 |
| $table->float('amount', 8, 2); | 相当于 FLOAT 型态,总共有 8 位数,在小数点后面有 2 位数。 |
| $table->increments('id'); | 递增的 ID (主键),使用相当于「UNSIGNED INTEGER」的型态。 |
| $table->integer('votes'); | 相当于 INTEGER 型态。 |
| $table->ipAddress('visitor'); | 相当于 IP 地址形态。 |
| $table->json('options'); | 相当于 JSON 型态。 |
| $table->jsonb('options'); | 相当于 JSONB 型态。 |
| $table->longText('description'); | 相当于 LONGTEXT 型态。 |
| $table->macAddress('device'); | 相当于 MAC 地址形态。 |
| $table->mediumIncrements('id'); | 递增 ID (主键) ,相当于「UNSIGNED MEDIUM INTEGER」型态。 |
| $table->mediumInteger('numbers'); | 相当于 MEDIUMINT 型态。 |
| $table->mediumText('description'); | 相当于 MEDIUMTEXT 型态。 |
| $table->morphs('taggable'); | 加入整数 taggable_id 与字符串 taggable_type。 |
| $table->nullableMorphs('taggable'); | 与 morphs() 字段相同,但允许为NULL。 |
| $table->nullableTimestamps(); | 与 timestamps() 相同,但允许为 NULL。 |
| $table->rememberToken(); | 加入 remember_token 并使用 VARCHAR(100) NULL。 |
| $table->smallIncrements('id'); | 递增 ID (主键) ,相当于「UNSIGNED SMALL INTEGER」型态。 |
| $table->smallInteger('votes'); | 相当于 SMALLINT 型态。 |
| $table->softDeletes(); | 加入 deleted_at 字段用于软删除操作。 |
| $table->string('email'); | 相当于 VARCHAR 型态。 |
| $table->string('name', 100); | 相当于 VARCHAR 型态,并带有长度。 |
| $table->text('description'); | 相当于 TEXT 型态。 |
| $table->time('sunrise'); | 相当于 TIME 型态。 |
| $table->timeTz('sunrise'); | 相当于 TIME (带时区) 形态。 |
| $table->tinyInteger('numbers'); | 相当于 TINYINT 型态。 |
| $table->timestamp('added_on'); | 相当于 TIMESTAMP 型态。 |
| $table->timestampTz('added_on'); | 相当于 TIMESTAMP (带时区) 形态。 |
| $table->timestamps(); | 加入 created_at 和 updated_at 字段。 |
| $table->timestampsTz(); | 加入 created_at and updated_at (带时区) 字段,并允许为NULL。 |
| $table->unsignedBigInteger('votes'); | 相当于 Unsigned BIGINT 型态。 |
| $table->unsignedInteger('votes'); | 相当于 Unsigned INT 型态。 |
| $table->unsignedMediumInteger('votes'); | 相当于 Unsigned MEDIUMINT 型态。 |
| $table->unsignedSmallInteger('votes'); | 相当于 Unsigned SMALLINT 型态。 |
| $table->unsignedTinyInteger('votes'); | 相当于 Unsigned TINYINT 型态。 |
| $table->uuid('id'); | 相当于 UUID 型态。 |
利用Homestead快速运行一个Laravel项目的方法详解这篇文章主要给大家介绍了关于利用Homestead如何快速运行一个Laravel项目的相关资料,文中通过示例代码介绍的非常详
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解这篇文章主要给大家介绍了关于Laravel中Intervention/image图片处理扩展包的安装、使用与在使用可能遇到的坑的解决方法
Laravel下生成验证码的类这篇文章主要为大家详细介绍了Laravel下生成验证码的类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
详解php语言最牛掰的Laravel框架Laravel以其简洁、优雅的特性赢得了大家的广泛关注,无论是专家还是新手,在开发PHP项目的时候,都会第一时间的想
Laravle eloquent 多对多模型关联实例详解Eloquent中一个模型就是一个数据表,数据表之间通常会有关联,多对多关联就是2个表之间相互有很多关联,这篇文章给大
Laravel 批量更新多条数据的示例本篇文章主要介绍了Laravel 批量更新多条数据的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起