沃梦达 / 编程问答 / php问题 / 正文

Laravel 迁移是如何工作的?

How do Laravel migrations work?(Laravel 迁移是如何工作的?)

本文介绍了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.

推荐答案

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

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 迁移是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 迁移是如何工作的?

基础教程推荐