接著上篇: http://www.itdecent.cn/p/7c884d23f632
本篇把第一節(jié)中的數(shù)據(jù)庫使用laravel的數(shù)據(jù)庫遷移,遷入數(shù)據(jù)庫。
書寫數(shù)據(jù)遷移文件
導(dǎo)入項目到phpstrom中,會在database 下面看見 migrations文件夾,默認會有如下兩個文件
<pre>
2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resets_table.php
</pre>
內(nèi)容分別為
** 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>
遷移文件結(jié)構(gòu)框架
通過默認的文件看,遷移文件的結(jié)構(gòu)大致 如下:
<pre>
class XXXXXXTable extends Migration
{
public function up()//遷移創(chuàng)建
{
Schema::create('password_resets', function (Blueprint $table) {
//創(chuàng)建或者修改字段
});
}
public function down()//回滾等時候調(diào)用
{
Schema::dropIfExists('password_resets');//舍棄數(shù)據(jù)表
}
}
</pre>
創(chuàng)建posts表數(shù)據(jù)遷移文件
看了上面的大致結(jié)構(gòu),其實我們是可以自己寫的,但是,為了省力,laravel框架為我們已經(jīng)提供了很方便的命令
<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>
不要怕折騰?。。?/p>
我查詢了一些資料,有說是字段不對,有說是 存儲引擎不對!還有說是遷移命令執(zhí)行的時候,表之間有依賴關(guān)系,需要控制表的執(zhí)行順序?。?!
但是,我最后發(fā)現(xiàn)是默認 increments 默認生產(chǎn)的id都是無符號的,所以,這里要保持一致(截圖只是示意,例如uid那個unsigned我是加了的)!

如下
<pre>
$table->integer('post_id')->unsigned()->default(0);
</pre>
到這里,數(shù)據(jù)表制作好了!
對了,如果中間遇到錯誤 可以運行rollback命令,也可以手動暴力刪除數(shù)據(jù)庫所有表。
連接laravel 數(shù)據(jù)庫
認真安裝過homestead的同學(xué),肯定知道如何連接了 ,配置如下
ip:127.0.0.1
端口:33060
用戶名:homestead
密碼:homestead
使用sequrl pro 鏈接如下

還不錯,laravel考慮的還是比較多的,不愧是藝術(shù)家的框架!
感興趣的可以加入Laravel 學(xué)習(xí) qq群:307317509 和大家一起進步!加入注明:Laravel博客學(xué)習(xí)