Laravel开发项目数据库建表,可以用原生的创建,也可以使用Laravel数据迁移来实现。Laravel的数据迁移不熟的时候,用起来很困难,但是通过数据迁移开发的项目在项目移植上优势非常大。
Laravel创建数据迁移是通过Laravel的artisan命令来实现的.
首先,创建数据迁移文件:
php artisan make:migration create_tests_table --create=tests
友情提示:千万别加分号 ( ;)
创建成功提示:
Created Migration: 2016_03_07_144753_create_tests_table
这就表示数据迁移文件创建成功了,创建成功的迁移文件会在databases/migrations
下生成。
生成的文件代码:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tests');
}
}
类中up()方法是创建表的,默认情况下会生成三个字段
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
生成sql语句是:
create table tests (id int(10) unsigned primary key,`created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci
__ 检测表__
检测表/列是否存在
if (Schema::hasTable('users')) {
//
}
if (Schema::hasColumn('users', 'email')) {
//
}
指定数据库引擎
Schema::create('users', function ($table) {
$table->engine = 'InnoDB';
$table->increments('id');
});
重命名/删除表
重命名
Schema::rename($from, $to);
删除表:
Schema::drop('users');
Schema::dropIfExists('users');
创建列
一些支持的语法:
语句 语法
$table->bigIncrements('id'); 自增主键,bigint无符号类型
$table->bigInteger('votes'); bigint类型
$table->binary('data'); Blog类型
$table->boolean('confirmed'); 布尔类型
$table->char('name', 4); char(4),字段名为name
$table->date('created_at'); date类型,字段名为created_at
$table->dateTime('created_at'); datetime类型
$table->decimal('amount', 5, 2); decimal(5,2)
$table->double('column', 15, 8); double(15,8)
$table->enum('choices', ['foo', 'bar']); 枚举类型
$table->float('amount'); float类型
$table->increments('id'); int类型自增主键id,int无符号类型
$table->integer('votes'); int类型,int(11)
$table->json('options'); json类型
$table->jsonb('options'); jsonb类型
$table->longText('description'); longtext类型,长文本类型
$table->mediumInteger('numbers'); mediumint类型
$table->mediumText('description'); mediumtext类型
$table->morphs('taggable'); 添加一个 INTEGER类型的 taggable_id 列和一个 STRING类型的 taggable_type列
$table->nullableTimestamps(); 允许为空的timestamp
$table->rememberToken(); 添加一个列,varchar(100) null
$table->smallInteger('votes'); smallint
$table->softDeletes(); 添加一个列deleted_at
$table->string('email'); varchar
$table->string('name', 100); varchar(100)
$table->text('description'); text
$table->time('sunrise'); time
$table->tinyInteger('numbers'); tinyint
$table->timestamp('added_on'); timestamp
$table->timestamps(); 添加两个列created_at,updated_at,timestamp类型
$table->uuid('id'); uuid唯一值的
有强迫症吗?有。当然可以像mysql那样,把列添加在某个你想要的位置,当然也还有其他功效。
语法 功能
->first() 作为第一列(仅在mysql下有效)
->after('column') 某列之后添加列(仅在mysql下有效)
->nullable() 允许null值
->default($value) 设置一个默认值
->unsigned() 设置一个int家族的列为无符号型
示例:
$table->integer('id')->unsigned()
迁移文件写完之后,执行命令,即可创建数据库表:
php artisan migrate
建表的时候创建错了怎么办?
修改列
使用数据迁移修改列需要一个依赖,laravel默认好像是没有安装,doctrine/dbal
,如果这个依赖没有安装,执行下面语句进行安装,在项目composer.json
所在目录下执行即可。
composer require doctrine/dbal
我们创建个迁移文件修改列:
php artisan make:migration change_cloumn_to_tests_table --table=tests
修改文件代码 up()方法:
public function up()
{
Schema::table('tests', function (Blueprint $table) {
$table->string('name',60)->change();
});
}
再次执行迁移 php artisan migrate
即可发现列长度变成60了。
我们同样可以重命名列,还是修改up方法:
public function up()
{
Schema::table('tests', function (Blueprint $table) {
$table->renameColumn('name', 'age');
});
}
执行迁移,php artisan migrate
删除列
public function up()
{
Schema::table('tests', function (Blueprint $table) {
$table->dropColumn('age');
});
}
索引
创建唯一索引
$table->string('name')->unique(); //在创建字段的时候
$table->unique('name'); //字段创建完成之后
普通索引
$table->index('name');
$table->index(['account_id', 'created_at']);
支持的几种索引类型:
语法 类型
$table->primary('id'); 主键
$table->primary(['first', 'last']); 联合主键索引
$table->unique('email'); 唯一索引
$table->index('state'); 普通索引
删除索引
语法 类型
$table->dropPrimary('users_id_primary'); 删除users表id主键索引
$table->dropUnique('users_email_unique'); 删除users表email字段唯一索引
$table->dropIndex('geo_state_index'); 删除geo表state的普通索引
外键约束
为posts表,创建一个外键列,关联users表的id字段:
Schema::table('posts', function ($table) {
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
你还可以为约束的“on delete”和“on update”属性指定期望的动作:
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
删除外键约束
$table->dropForeign('posts_user_id_foreign');