Laravel 迁移是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30220377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do Laravel migrations work?
提问by Recur
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.
我对这种类型的框架完全陌生。我来自准系统 PHP 开发,我似乎无法找到一个易于理解的指南,迁移实际上做了什么。
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.
我正在尝试创建一个已有数据库的项目。我使用过这个:https: //github.com/Xethron/migrations-generator[1]但是通过迁移对模式进行更改似乎会吐出错误,这意味着我不知道我在做什么。
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.
我真的需要简单了解一下迁移的实际作用、它们如何影响数据库以及您认为对绝对初学者有帮助的任何其他内容。
回答by Adnan
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 Builderto easily manage your application's schema.
迁移是数据库的一种版本控制。它们允许团队修改数据库架构并保持最新的当前架构状态。迁移通常与Schema Builder配对,以轻松管理应用程序的架构。
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:
通过迁移,您不需要在 phpMyAdmin 中创建表,您可以在 Laravel 中完成。以下是创建用户表的示例:
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');
}
}
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.
在这段代码中,我们创建了包含“姓名”、“姓氏”等字段的表……我们在 Laravel 代码中说过,当迁移完成时,它们是字符串类型,我们在数据库中有包含这些字段的完整表。
Run a migration to create table
运行迁移以创建表
To create a migration, you may use the make:migration command on the Artisan CLI(artisan command line interface):
要创建迁移,您可以在Artisan CLI(artisan 命令行界面)上使用 make:migration 命令:
php artisan make:migration create_users_table
or
或者
php artisan make:migration create_users_table --create=users
Run a migration to alter table
运行迁移以更改表
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
当您需要在数据库表中做一些更改示例时:将字段投票添加到用户表中,您可以在 Laravel 代码中这样做,而无需触及 SQL 代码
php artisan make:migration add_votes_to_users_table --table=users
Rollback the last migration operation
回滚上次迁移操作
If you make mistake and did something wrong you can always rollback to return database in previous state.
如果您犯了错误并做错了事,您可以随时回滚以返回先前状态的数据库。
php artisan migrate:rollback
Rollback all migrations
回滚所有迁移
php artisan migrate:reset
Rollback all migrations and run them all again
回滚所有迁移并再次运行它们
php artisan migrate:refresh
php artisan migrate:refresh --seed
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.
迁移的最大优势之一是在不接触 SQL 代码的情况下创建数据库。您可以在 PHP 代码中创建具有关系的整个数据库,然后将其迁移到 MySQL、PL/SQL、MSSQL 或任何其他数据库中。
Also I recommend the free Laravel 5 fundamental series, in episode 7 you can hear more about migrations.
另外我推荐免费的Laravel 5 基础系列,在第 7 集中你可以听到更多关于迁移的信息。
回答by Kryten
A simple explanation of migrations:
迁移的简单解释:
They're a versioning system for your database scheme.
它们是您的数据库方案的版本控制系统。
Imagine you're setting up a new application. The first thing you do is create a table (call it mytable
) with a couple of columns. This will be your first migration. You run the migration (php artisan migrate
) when you start working on your application and voila! You have a new table in your database.
假设您正在设置一个新应用程序。您要做的第一件事是创建一个mytable
包含几列的表(称为)。这将是您的第一次迁移。php artisan migrate
当您开始处理您的应用程序时,您将运行迁移 ( ),瞧!您的数据库中有一个新表。
Some time later, you decide that you need a new column in your table. You create a migration (php artisan make:migration
in Laravel 5) and a new migration file is created for you. You update the code in that migration, run php artisan migrate
again, and you have a new column in your table.
一段时间后,您决定在表中需要一个新列。您创建一个迁移(php artisan make:migration
在 Laravel 5 中)并为您创建一个新的迁移文件。您更新该迁移中的代码,php artisan migrate
再次运行,您的表中有一个新列。
Ok, that's the basic idea behind migrations. But there's more...
好的,这就是迁移背后的基本思想。但还有更多...
Suppose, later on, you realize that you messed up when you created that last migration. You've already run it, so your database has changed. How to you fix it? Well, if you wrote your migration correctly and implemented the down
method, you can just do php artisan migrate:rollback
and it will rollback the migration.
假设,稍后,您意识到在创建最后一次迁移时搞砸了。您已经运行了它,因此您的数据库已更改。你怎么修?好吧,如果您正确地编写了迁移并实现了该down
方法,您就可以这样做php artisan migrate:rollback
,它会回滚迁移。
How does Laravel do this? By keeping track of your migrations in a special database table. The php artisan migrate:install
command will set things up for you so Laravel can manage these migrations.
Laravel 如何做到这一点?通过在特殊的数据库表中跟踪您的迁移。该php artisan migrate:install
命令将为您设置好东西,以便 Laravel 可以管理这些迁移。
Over time, as your application grows, you will add more and more migrations. Laravel gives you a way to step forward and back through your migrations as needed to ensure your database is at whatever state you need it to be as you're working.
随着时间的推移,随着应用程序的增长,您将添加越来越多的迁移。Laravel 为您提供了一种根据需要在迁移中前进和后退的方法,以确保您的数据库在您工作时处于您需要的任何状态。
Check out the list of artisan
commands with php artisan
. You can also request help on a particular command with php artisan help <command>
.
使用 来查看artisan
命令列表php artisan
。您还可以使用 请求有关特定命令的帮助php artisan help <command>
。
回答by user1669496
Imagine you are working on a project with other developers. As developers are adding more and more features, they are also required to add more and more tables, columns, remove columns, change column data types, etc...
想象一下,您正在与其他开发人员一起开展一个项目。随着开发人员添加越来越多的功能,他们也需要添加越来越多的表、列、删除列、更改列数据类型等......
This can very quickly and easily get out of control. If you missed some SQL from one developer, another developer's SQL may not work correctly. It also potentially becomes a huge time waster trying to sort through a bunch of sql files trying to figure out which ones you've missed. It's only a matter of time till everyone on the development team is working with different databases or god forbid, someone breaks the production database. With migrations, you just need to run php artisan migrate
in the command line and all changes to the database will be taken care of for you.
这会非常快速且容易地失控。如果您遗漏了一位开发人员的某些 SQL,则另一位开发人员的 SQL 可能无法正常工作。尝试对一堆 sql 文件进行排序以找出您错过了哪些 sql 文件,这也可能会浪费大量时间。开发团队中的每个人都在使用不同的数据库或上帝保佑,这只是时间问题,有人破坏了生产数据库。使用迁移,您只需要php artisan migrate
在命令行中运行,数据库的所有更改都会为您处理。
This is basically why migrations are useful I'm not going to go into the basics of how they work because Kryten has a pretty good write-up here already.
这基本上就是迁移有用的原因我不打算深入介绍它们如何工作的基础知识,因为 Kryten 已经在这里写了一篇很好的文章。