Trong khi, các phương pháp
Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
6 và Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
7 vẫn được áp dụng, Rails 3. 1 nhận được một phương thức Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
8 "biết cách di chuyển cơ sở dữ liệu của bạn và đảo ngược nó khi quá trình di chuyển được khôi phục mà không cần phải viết một phương thức riêng"Xem "Di chuyển bản ghi đang hoạt động" để biết thêm thông tin
rails g migration FixColumnName
class FixColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
Nếu bạn tình cờ có cả đống cột cần đổi tên hoặc thứ gì đó yêu cầu lặp đi lặp lại tên bảng
rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...
Bạn có thể sử dụng
Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
9 để giữ mọi thứ gọn gàng hơnclass FixColumnNames < ActiveRecord::Migration
def change
change_table :table_name do |t|
t.rename :old_column1, :new_column1
t.rename :old_column2, :new_column2
...
end
end
end
Sau đó, chỉ cần
class RenameOldTableToNewTable < ActiveRecord::Migration
def self.up
rename_table :old_table_name, :new_table_name
end
def self.down
rename_table :new_table_name, :old_table_name
end
end
0 như bình thường hoặc bất cứ điều gì bạn tiếp tục công việc kinh doanh của mìnhĐối với đường ray 4
Trong khi tạo một
class RenameOldTableToNewTable < ActiveRecord::Migration
def self.up
rename_table :old_table_name, :new_table_name
end
def self.down
rename_table :new_table_name, :old_table_name
end
end
1 để đổi tên một cột, Rails 4 tạo ra một phương thức Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
8 thay vì Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
6 và Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
7 như đã đề cập trong phần trên. Phương thức Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
8 được tạo là Trình tạo di chuyển cho Laravel là một gói của Bennett Treptow để tạo di chuyển từ các cấu trúc cơ sở dữ liệu hiện có
Trường hợp sử dụng chính cho gói này sẽ là một dự án có nhiều lần di chuyển làm thay đổi các bảng bằng cách sử dụng ->change[] từ học thuyết/dbal mà SQLite không hỗ trợ và cần một cách để cập nhật cấu trúc bảng cho SQLite để sử dụng trong các thử nghiệm. Một trường hợp sử dụng khác là lấy một dự án có cơ sở dữ liệu và không có di chuyển và biến cơ sở dữ liệu đó thành di chuyển cơ sở
Gói này có thể hữu ích nếu bạn đang chuyển một ứng dụng hiện có sang Laravel và muốn tạo lại quá trình di chuyển cơ sở dữ liệu cho ứng dụng để trợ giúp phát triển và thử nghiệm
Liên quan Tạo Password Generator với Laravel
Để hình dung quá trình này hoạt động như thế nào, readme có cách sử dụng ví dụ xác định bảng
Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
7 sauSchema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
3Sử dụng gói này, bạn có thể chạy lệnh sau để tạo lớp bản thiết kế dựa trên định nghĩa bảng
Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
4Và kế hoạch chi tiết dẫn xuất từ bảng
Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
7 sẽ trông như sau theo ví dụSchema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
5Gói này cũng đi kèm với các sơ đồ di chuyển bảng và xem khác nhau và cài đặt cấu hình. Ví dụ: một cấu hình xác định các mẫu tên tệp được sử dụng để tạo lược đồ bảng
Schema::create['table_name', function [Blueprint $table] {
$table->increments['id'];
$table->string['field_2'];
$table->string['field_3'];
$table->string['field_4'];
$table->date['field_5'];
$table->string['field_6'];
$table->timestamps[];
}];
6Tại thời điểm viết gói hỗ trợ MySQL, nhưng cũng có thể hỗ trợ Postgres, SQLite và SQL Server theo readme
Bạn có thể tìm hiểu thêm về gói này, nhận hướng dẫn cài đặt đầy đủ và xem mã nguồn trên GitHub
Gói này đã được gửi đến phần Liên kết Tin tức Laravel của chúng tôi. Liên kết là nơi cộng đồng có thể đăng các gói và hướng dẫn xung quanh hệ sinh thái Laravel. Theo dõi trên Twitter @LaravelLinks