Laravel 5框架学习之数据库迁移(Migrations)
《PHP实战:Laravel 5框架学习之数据库迁移(Migrations)》要点: PHP实例database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. PHP实例在 database/migrations 目录中包含两个迁移文件,一个建立用户表,一个用于用户暗码重置. PHP实例在迁移文件中,up 办法用于创建数据表,down办法用于回滚,也就是删除数据表. PHP实例执行数据库迁移
代码如下:
php artisan migrate #输出 Migration table created successfully. Migrated: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_100000_create_password_resets_table PHP实例查看mysql数据库,可以看到产生了三张表. migratoins 表是迁移记录表,users 和 pasword_resets. PHP实例如果设计有问题,执行数据库回滚
代码如下:
php artisan migrate:rollback #输出 Rolled back: 2014_10_12_100000_create_password_resets_table Rolled back: 2014_10_12_000000_create_users_table PHP实例再次查看mysql数据库,就剩下 migrations 表了,users password_resets 被删除了. PHP实例修改迁移文件,再次执行迁移. PHP实例新建迁移
代码如下:
php artisan make:migration create_article_table --create='articles' #输出 Created Migration: 2015_03_28_050138_create_article_table PHP实例在 database/migrations 下生成了新的文件. PHP实例
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateArticleTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles',function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('articles');
}
}
PHP实例自动添加了 id列,自动增长,timestamps() 会自动产生 created_at 和 updated_at 两个时间列.我们添加一些字段: PHP实例
public function up()
{
Schema::create('articles',function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamp('published_at');
$table->timestamps();
});
}
PHP实例执行迁移:
代码如下:
php artisan migrate PHP实例现在有了新的数据表了. PHP实例假设我们需要添加一个新的字段,你可以回滚,然后修改迁移文件,再次执行迁移,或者可以直接新建一个迁移文件
代码如下:
php artisan make:migration add_excerpt_to_articels_table PHP实例查看新产生的迁移文件 PHP实例
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class AddExcerptToArticelsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
PHP实例只有空的 up 和 down 办法.我们可以手工添加代码,或者我们让laravel为我们生成基础代码.删除这个文件,重新生成迁移文件,注意添加参数:
代码如下:
php artisan make:migration add_excerpt_to_articels_table --table='articles' PHP实例现在,up 办法里面有了初始代码. PHP实例
public function up()
{
Schema::table('articles',function(Blueprint $table)
{
//
});
}
PHP实例添加实际的数据修改代码: PHP实例
public function up()
{
Schema::table('articles',function(Blueprint $table)
{
$table->text('excerpt')->nullable();
});
}
public function down()
{
Schema::table('articles',function(Blueprint $table)
{
$table->dropColumn('excerpt');
});
}
PHP实例nullable() 表示字段也可以为空. PHP实例再次执行迁移并检查数据库. PHP实例如果我们为了好玩,执行回滚
代码如下:
php artisan migrate:rollback PHP实例excerpt 列没有了. PHP实例以上所述就是本文的全部内容了,希望能够给大家熟练掌握Laravel5框架有所赞助. 欢迎参与《PHP实战:Laravel 5框架学习之数据库迁移(Migrations)》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |