Schema::create('users', function($table) { $table->increments('id'); $table->string('username', 32); $table->string('email', 320); $table->string('password', 60); $table->timestamps(); });
将建立如下数据库表:mysql
mysql> describe users; +------------+------------------+-----+----------------+ | Field | Type | Key | Extra | +------------+------------------+-----+----------------+ | id | int(10) unsigned | PRI | auto_increment | | username | varchar(32) | | | | email | varchar(320) | | | | password | varchar(60) | | | | created_at | timestamp | | | | updated_at | timestamp | | | +------------+------------------+-----+----------------+
Schema::rename($from, $to);
Schema::drop('users'); Schema::dropIfExists('users'); // 建议使用这种方式
Schema::connection('foo')->create('users', function($table) { $table->increments('id'): });
$table->increments('id'); // INTEGER 类型 $table->bigIncrements('id'); // BIGINT 类型
$table->integer('votes'); // INTEGER 类型 $table->bigInteger('votes'); // BIGINT 类型 $table->smallInteger('votes'); // SMALLINT 类型 $table->tinyInteger('votes'); // TINYINT 类型
注意: tinyInteger
仅 MySQL
SqlServer
支持, Postgres
会将其识别为 smallint
,而 SQLite
则是只有 integer
类型。sql
$table->float('amount'); // FLOAT 类型 $table->decimal('amount', 5, 2); // 带精度和小数的 DECIMAL
$table->string('email'); // VARCHAR 类型 $table->string('name', 100); // VARCHAR 类型(带长度) $table->text('description'); // TEXT 类型 $table->binary('data'); // BLOB 类型
$table->boolean('confirmed'); // BOOLEAN 类型
$table->enum('choices', array('foo', 'bar')); // ENUM 类型
$table->date('created_at'); // DATE 类型 $table->dateTime('created_at'); // DATETIME 类型 $table->time('sunrise'); // TIME 类型 $table->timestamp('added_on'); // TIMESTAMP 类型
$table->timestamps(); // 添加 created_at 和 updated_at 列 $table->softDeletes(); // 添加 deleted_at 列用于软删除
->nullable(); // 指明该字段容许 NULL 值 ->default($value); // 为字段声明一个默认值 ->unsigned(); // 设置 INTEGER 为无符号
$table->string('email')->after('name'); // 指明字段排序 $table->string('email')->comment('电子邮件'); // 字段注释(非原生)
注意: 关于字段注释,请参考 让 Laravel 4.1 的“数据库迁移”支持 MySQL 字段注释 。数据库
Schema::table('users', function($table) { $table->renameColumn('from', 'to'); });
注意: 不支持重命名 enum
字段类型。post
从表中删除一个字段:.net
Schema::table('users', function($table) { $table->dropColumn('votes'); });
从表中删除多个字段:code
Schema::table('users', function($table) { $table->dropColumn('votes', 'avatar', 'location'); });
if (Schema::hasTable('users')) { // }
if (Schema::hasColumn('users', 'email')) { // }
$table->primary('id'); // 添加一个主键 $table->primary(array('first', 'last')); // 添加组合键 $table->unique('email'); // 添加惟一键 $table->index('state'); // 添加一个索引
支持链式调用:blog
$table->string('email')->unique();
$table->dropPrimary('users_id_primary'); // 从 "users" 表中删除一个 主键 $table->dropUnique('users_email_unique'); // 从 "users" 表中删除一个 惟一键 $table->dropIndex('geo_state_index'); // 从 "geo" 表中删除一个 索引
指定 user_id
字段参照 users
表中的 id
字段:排序
$table->foreign('user_id')->references('id')->on('users'); $table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade'); $table->foreign('user_id')->references('id')->on('users') ->onUpdate('cascade');
注意: 当建立一个参照递增整数类型的外键的时候,记得把外键字段的类型定义为无符号。索引
$table->dropForeign('posts_user_id_foreign');
Schema::create('users', function($table) { $table->engine = 'InnoDB'; $table->string('email'); });