Laravel迁移如何工作? [英] How do Laravel migrations work?

查看:95
本文介绍了Laravel迁移如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这种类型的框架完全陌生.我来自准系统PHP开发,似乎找不到易于理解的指南,说明迁移实际上是做什么的.

I'm totally new to this type of framework. I've come from barebones PHP development and I can't seem to find an easy to understand guide what migrations actually do.

我正在尝试创建一个已经具有现有数据库的项目.我使用了它: https://github.com/Xethron/migrations-generator[1] ,但是通过迁移对架构进行更改似乎会吐出错误,这意味着我不知道自己在做什么.

I'm trying to create a project that already has an existing database. I've used this: https://github.com/Xethron/migrations-generator[1] but making changes to the schema via the migrations seems to spit out errors which means I have no idea what I'm doing.

我真的需要简单了解迁移的实际作用,迁移对数据库的影响以及您认为对初学者有帮助的其他任何事情.

I really need a simple run down of what migrations actually do, how they affect the database and anything else you think would help an absolute beginner.

推荐答案

迁移是数据库的一种版本控制.他们允许团队修改数据库架构并保持最新架构状态.迁移通常与模式生成器配对,以轻松管理应用程序的模式.

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.

通过迁移,您无需在phpMyAdmin中创建表,您可以在Laravel中进行.这是创建用户表的示例:

With migrations you don't need to create table in phpMyAdmin, you can do it in Laravel. Here is an example to create a user table:

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id'); // autoincrement id field
            $table->string('name');   // string field
            $table->string('lastname');
            $table->string('title');
            $table->string('email')->unique();   // unique string field
            $table->string('password', 60);      // string field with max 60 characters
            $table->boolean('Status')->default(0); // string field with default value 0
            $table->timestamps();

        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}

在此代码中,我们创建具有名称",姓氏"之类的字段的表...在Laravel代码中,我们说过迁移完成后它们是字符串类型的,我们已经在数据库中使用了具有此字段的完整表.

I this code we create table with fields like "name", "lastname"... we said in our Laravel code they are string type when migration is done we have complete table in databese with this fields.

运行迁移以创建表

要创建迁移,可以在 Artisan CLI (artisan命令行界面)上使用make:migration命令:

To create a migration, you may use the make:migration command on the Artisan CLI (artisan command line interface):

php artisan make:migration create_users_table

php artisan make:migration create_users_table --create=users

运行迁移以更改表

当您需要在数据库表示例中进行一些更改时:将字段投票添加到用户表中,您可以在Laravel代码中做到这一点,而无需触摸SQL代码

When you need to do some changes in database table example: add field vote to user table you can do like this in your Laravel code without touching SQL code

php artisan make:migration add_votes_to_users_table --table=users

回滚上一次迁移操作

如果您犯错并且做错了什么,您总是可以回滚以返回先前状态的数据库.

If you make mistake and did something wrong you can always rollback to return database in previous state.

php artisan migrate:rollback

回滚所有迁移

php artisan migrate:reset

回滚所有迁移,然后再次运行它们

php artisan migrate:refresh

php artisan migrate:refresh --seed

迁移的最大优势之一是无需触摸SQL代码即可创建数据库.您可以使用PHP代码创建具有关系的整个数据库,然后将其迁移到MySQL,PL/SQL,MSSQL或任何其他数据库中.

One of best advantage of migrations are creating database without touching SQL code. You can make whole database with relationship in PHP code then migrate it into MySQL, PL/SQL, MSSQL or any other database.

我也推荐免费的 Laravel 5基本系列 7您可以了解有关迁移的更多信息.

Also I recommend the free Laravel 5 fundamental series, in episode 7 you can hear more about migrations.

这篇关于Laravel迁移如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆