码迷,mamicode.com
首页 > 其他好文 > 详细

laravel框架的数据迁移 (摘取)

时间:2019-12-11 21:39:56      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:名称   div   creat   生成器   user   func   name   方法   xtend   

生成迁移

使用 Artisan make:migration 来创建迁移:

php artisan make:migration create_users_table

 

新的迁移文件会被放置在 database/migrations 目录中。每个迁移文件的名称都包含了一个时间戳,以便让 Laravel 确认迁移的顺序。

--table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时是否将创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表:

  

php artisan make:migration create_users_table --create=users
 
php artisan make:migration add_votes_to_users_table --table=users

 

如果你想为生成的迁移指定一个自定义输出路径,则可以在运行 make:migration 命令时添加 --path 选项。给定的路径必须是相对于应用程序的基本路径。

迁移结构

迁移类通常会包含两个方法:up 和 downup 方法可为数据库添加新的数据表、字段或索引,而 down方法则是 up 方法的逆操作。

你可以在这两个方法中使用 Laravel 数据库结构生成器来创建以及修改数据表。若要了解 Schema 生成器中的所有可用方法,可查询文档。以下的迁移实例会创建一张 flights 数据表:

  

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
33
<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateFlightsTable extends Migration
{
    /**
     * 运行数据库迁移
     *
     * @return void
     */
    public function up()
    {
        Schema::create(flights, function (Blueprint $table) {
            $table->increments(id);
            $table->string(name);
            $table->string(airline);
            $table->timestamps();
        });
    }
 
    /**
     * 回滚数据库迁移
     *
     * @return void
     */
    public function down()
    {
        Schema::drop(flights);
    }
}

 

运行迁移

使用 Artisan 命令 migrate 来运行所有未完成的迁移:

php artisan migrate

在生产环境强制执行迁移

一些迁移操作是具有破坏性的,这意味着可能会导致数据丢失。为了防止有人在生产环境中运行这些命令,系统会在这些命令被运行之前与你进行确认。如果要强制忽略系统的提示运行命令,则可以使用 --force 标记:

php artisan migrate --force

laravel框架的数据迁移 (摘取)

标签:名称   div   creat   生成器   user   func   name   方法   xtend   

原文地址:https://www.cnblogs.com/Rawan/p/12024983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!