接着上篇: http://www.jianshu.com/p/7c884d23f632
本篇把第一节中的数据库使用laravel的数据库迁移,迁入数据库。
书写数据迁移文件
导入项目到phpstrom中,会在database 下面看见 migrations文件夹,默认会有如下两个文件
<pre>
2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resets_table.php
</pre>
内容分别为
** 2014_10_12_000000_create_users_table.php **
<pre>
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
</pre>
** 2014_10_12_100000_create_password_resets_table.php **
<pre>
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
</pre>
迁移文件结构框架
通过默认的文件看,迁移文件的结构大致 如下:
<pre>
class XXXXXXTable extends Migration
{
public function up()//迁移创建
{
Schema::create('password_resets', function (Blueprint $table) {
//创建或者修改字段
});
}
public function down()//回滚等时候调用
{
Schema::dropIfExists('password_resets');//舍弃数据表
}
}
</pre>
创建posts表数据迁移文件
看了上面的大致结构,其实我们是可以自己写的,但是,为了省力,laravel框架为我们已经提供了很方便的命令
<pre>php artisan make:migration posts </pre>
http://d.laravel-china.org/docs/5.4/migrations#creating-columns
我这里直接粘贴我的迁移文件.
提示:所有的迁移命令请在homestead的虚拟机中运行
** php artisan make:migration posts **
<pre>
class Posts extends Migration
{
private $table_name = 'posts';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create($this->table_name, function (Blueprint $table) {
$table->increments('id');
$table->integer('uid')->unsigned();
$table->string('title')->unique();
$table->text('content')->defalult('');
$table->text('description')->defalult('');
$table->tinyInteger('status')->default(1);
$table->timestamps();
});
Schema::table($this->table_name, function($table) {
$table->foreign('uid')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop($this->table_name);
}
}
</pre>
** php artisan make:migration comments **
<pre>
class Comments extends Migration
{
private $table_name = 'comments';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create($this->table_name, function (Blueprint $table) {
$table->increments('id');
$table->integer('uid')->unsigned();
$table->integer('post_id')->default(0);
$table->string('title')->unique();
$table->text('content')->defalult('');
$table->tinyInteger('status')->default(1);
$table->timestamps();
});
Schema::table($this->table_name, function($table) {
$table->foreign('uid')->references('id')->on('users')->onDelete('cascade');
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop($this->table_name);
}
}
</pre>
** php artisan make:migration users **
<pre>
class CreateUsersTable extends Migration
{
private $table_name = 'users';
/**
* Run the migrations.
*
* @return void
/
public function up()
{
Schema::create($this->table_name, function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('description')->default('');
$table->string('avatar')->default('');
$table->string('profile_image')->default('');
$table->tinyInteger('status')->default(1);
$table->rememberToken();
$table->timestamps();
});
}
/*
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists($this->table_name);
}
}
</pre>
运行迁移命令
<pre>
php artisan migrate
</pre>
不出意外
<pre>
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table comments
add constraint comments_post_id_foreign
foreign key (post_id
) references posts
(id
) on del
ete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
</pre>
不要怕折腾!!!
我查询了一些资料,有说是字段不对,有说是 存储引擎不对!还有说是迁移命令执行的时候,表之间有依赖关系,需要控制表的执行顺序!!!
但是,我最后发现是默认 increments 默认生产的id都是无符号的,所以,这里要保持一致(截图只是示意,例如uid那个unsigned我是加了的)!
Paste_Image.png如下
<pre>
$table->integer('post_id')->unsigned()->default(0);
</pre>
到这里,数据表制作好了!
对了,如果中间遇到错误 可以运行rollback命令,也可以手动暴力删除数据库所有表。
连接laravel 数据库
认真安装过homestead的同学,肯定知道如何连接了 ,配置如下
ip:127.0.0.1
端口:33060
用户名:homestead
密码:homestead
使用sequrl pro 链接如下
sequrl pro .png还不错,laravel考虑的还是比较多的,不愧是艺术家的框架!
感兴趣的可以加入Laravel 学习 qq群:307317509 和大家一起进步!加入注明:Laravel博客学习
网友评论