Laravel 5框架学习之数据库迁移(Migrations)

31 篇文章 2 订阅
8 篇文章 0 订阅
本文给大家介绍的是Laravel5框架中最强大的功能之一数据库迁移(database migrations),本文详细给大家介绍数据库迁移的步骤和方法,非常实用,有需要的小伙伴可以参考下。

database migrations 是laravel最强大的功能之一。数据库迁移可以理解为数据库的版本控制器。

在 database/migrations 目录中包含两个迁移文件,一个建立用户表,一个用于用户密码重置。

在迁移文件中,up 方法用于创建数据表,down方法用于回滚,也就是删除数据表。

执行数据库迁移

复制代码代码如下:

php artisan migrate
#输出
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

查看mysql数据库,可以看到产生了三张表。 migratoins 表是迁移记录表,users 和 pasword_resets。

如果设计有问题,执行数据库回滚

复制代码代码如下:

php artisan migrate:rollback
#输出
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table

再次查看mysql数据库,就剩下 migrations 表了, users password_resets 被删除了。

修改迁移文件,再次执行迁移。

新建迁移

复制代码代码如下:

php artisan make:migration create_article_table --create='articles'
#输出
Created Migration: 2015_03_28_050138_create_article_table

在 database/migrations 下生成了新的文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
 
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
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' );
  }
 
}

自动添加了 id列,自动增长,timestamps() 会自动产生 created_at 和 updated_at 两个时间列。我们添加一些字段:

?
1
2
3
4
5
6
7
8
9
10
11
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 artisan migrate

现在有了新的数据表了。

假设我们需要添加一个新的字段,你可以回滚,然后修改迁移文件,再次执行迁移,或者可以直接新建一个迁移文件

复制代码代码如下:

php artisan make:migration add_excerpt_to_articels_table

查看新产生的迁移文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
 
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class AddExcerptToArticelsTable extends Migration {
 
  /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
  //
  }
 
  /**
  * Reverse the migrations.
  *
  * @return void
  */
  public function down()
  {
  //
  }
 
}

只有空的 up 和 down 方法。我们可以手工添加代码,或者我们让laravel为我们生成基础代码。删除这个文件,重新生成迁移文件,注意添加参数:

复制代码代码如下:

php artisan make:migration add_excerpt_to_articels_table --table='articles'

现在,up 方法里面有了初始代码。

?
1
2
3
4
5
6
7
public function up()
{
Schema::table( 'articles' , function (Blueprint $table )
{
  //
});
}

添加实际的数据修改代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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' );
});
}

nullable() 表示字段也可以为空。

再次执行迁移并检查数据库。

如果我们为了好玩,执行回滚

复制代码代码如下:

php artisan migrate:rollback

excerpt 列没有了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值