問(wèn)題描述
我對(duì)這種類(lèi)型的框架完全陌生.我來(lái)自準(zhǔn)系統(tǒng) PHP 開(kāi)發(fā),我似乎找不到一個(gè)易于理解的指南,遷移實(shí)際上做了什么.
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.
我正在嘗試創(chuàng)建一個(gè)已有數(shù)據(jù)庫(kù)的項(xiàng)目.我用過(guò)這個(gè):https://github.com/Xethron/migrations-generator[1] 但通過(guò)遷移對(duì)架構(gòu)進(jìn)行更改似乎會(huì)吐出錯(cuò)誤,這意味著我不知道自己在做什么.
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.
我真的需要簡(jiǎn)單介紹一下遷移的實(shí)際作用、它們?nèi)绾斡绊憯?shù)據(jù)庫(kù)以及您認(rèn)為對(duì)絕對(duì)初學(xué)者有幫助的其他任何事情.
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.
推薦答案
遷移是數(shù)據(jù)庫(kù)的一種版本控制.它們?cè)试S團(tuán)隊(duì)修改數(shù)據(jù)庫(kù)架構(gòu)并保持最新的當(dāng)前架構(gòu)狀態(tài).遷移通常與 Schema Builder 配對(duì),以輕松管理應(yīng)用程序的架構(gòu).
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.
通過(guò)遷移,您不需要在 phpMyAdmin 中創(chuàng)建表,您可以在 Laravel 中完成.以下是創(chuàng)建用戶(hù)表的示例:
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');
}
}
在這段代碼中,我們創(chuàng)建了包含姓名"、姓氏"等字段的表……我們?cè)?Laravel 代碼中說(shuō)過(guò),當(dāng)遷移完成時(shí),它們是字符串類(lèi)型,我們?cè)跀?shù)據(jù)庫(kù)中有包含這些字段的完整表.
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.
運(yùn)行遷移以創(chuàng)建表
要?jiǎng)?chuàng)建遷移,您可以在 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
運(yùn)行遷移以更改表
當(dāng)你需要在數(shù)據(jù)庫(kù)表中做一些改變的例子:向用戶(hù)表添加字段投票時(shí),你可以在你的 Laravel 代碼中這樣做,而無(wú)需接觸 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
回滾上次遷移操作
如果你犯了錯(cuò)誤并且做錯(cuò)了什么,你總是可以回滾到以前狀態(tài)的數(shù)據(jù)庫(kù).
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
回滾所有遷移并再次運(yùn)行它們
php artisan migrate:refresh
php artisan migrate:refresh --seed
遷移的最大優(yōu)勢(shì)之一是在不接觸 SQL 代碼的情況下創(chuàng)建數(shù)據(jù)庫(kù).您可以在 PHP 代碼中創(chuàng)建具有關(guān)系的整個(gè)數(shù)據(jù)庫(kù),然后將其遷移到 MySQL、PL/SQL、MSSQL 或任何其他數(shù)據(jù)庫(kù)中.
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.
另外我推薦免費(fèi)的 Laravel 5 基礎(chǔ)系列,在劇集中7 您可以了解更多有關(guān)遷移的信息.
Also I recommend the free Laravel 5 fundamental series, in episode 7 you can hear more about migrations.
這篇關(guān)于Laravel 遷移是如何工作的?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!