問題描述
我對這種類型的框架完全陌生.我來自準系統 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 遷移是如何工作的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!