Bài viết này sẽ cung cấp một ví dụ về cách tạo bảng cơ sở dữ liệu bằng lệnh migration trong laravel 9. Mình sẽ hướng dẫn các bạn cách tạo migration trong laravel 9. Trong bài viết này, chúng ta sẽ thực hiện cách tạo table migration trong laravel 9. Bài đăng này sẽ cung cấp cho bạn một ví dụ đơn giản về cách tạo bảng trong laravel 9 bằng cách sử dụng migration
Mình sẽ hướng dẫn các bạn cách tạo bảng cơ sở dữ liệu bằng laravel migration. chúng tôi sẽ sử dụng lệnh laravel 9 để tạo di chuyển cho bảng
Trong ví dụ này, chúng tôi sẽ đơn giản tạo bảng "bài đăng" với các cột id, title, body, is_publish, created_at và updated_at. chúng tôi sẽ tạo di chuyển mới bằng lệnh laravel 9 và thêm các hàng ở trên. Sau đó, chúng tôi sẽ chạy di chuyển và chúng tôi sẽ tạo bảng trên cơ sở dữ liệu mysql. vì vậy hãy xem hướng dẫn dưới đây
Tạo di chuyển
Sử dụng lệnh dưới đây, bạn có thể chỉ cần tạo di chuyển cho bảng cơ sở dữ liệu
php artisan make:migration create_posts_table
Sau khi chạy lệnh trên, bạn có thể thấy tệp mới được tạo dưới đây và bạn phải thêm cột mới cho kiểu dữ liệu chuỗi, số nguyên, dấu thời gian và văn bản như dưới đây
Để thực hiện bất kỳ tác vụ nào, chúng tôi cần tạo một tệp di chuyển tương tự như những gì chúng tôi đã tạo trong khi tạo di chuyển
sự thay đổi duy nhất sẽ có trong tên di chuyển, luôn cố gắng viết tên di chuyển mang tính mô tả giúp laravel hiểu tên bảng trong quá trình di chuyển.
Dành cho e. g. Cập nhật cột
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
6 chúng ta nên chạy lệnh nhưphp artisan make:migration update_name_column_in_contacts_table
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Đọc thêm tại
Công cụ sửa đổi cột
Công cụ sửa đổi cột không là gì ngoài một chức năng được xác định trước có sẵn trong LARAVEL Migration bằng cách sử dụng chức năng đó, bạn có thể tạo bất kỳ cột nào
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
7, Đặt cột public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
8 và nhiều chức năng khác Di chuyển giống như kiểm soát phiên bản cho cơ sở dữ liệu của bạn, cho phép nhóm của bạn dễ dàng sửa đổi và chia sẻ lược đồ cơ sở dữ liệu của ứng dụng. Di chuyển thường được ghép nối với trình tạo lược đồ của Laravel để dễ dàng xây dựng lược đồ cơ sở dữ liệu của ứng dụng của bạn. Nếu bạn đã từng phải yêu cầu đồng đội thêm một cột vào lược đồ cơ sở dữ liệu cục bộ của họ theo cách thủ công, thì bạn đã gặp phải vấn đề mà việc di chuyển cơ sở dữ liệu giải quyết được
Mặt tiền
php artisan migrate:rollback
2 của Laravel cung cấp hỗ trợ cơ sở dữ liệu bất khả tri để tạo và thao tác các bảng trên tất cả các hệ thống cơ sở dữ liệu được hỗ trợ của LaravelTạo Migration
Để tạo di chuyển, hãy sử dụng lệnh Artisan
php artisan migrate:rollback
3php artisan make:migration create_users_table
Di chuyển mới sẽ được đặt trong thư mục
php artisan migrate:rollback
4 của bạn. Mỗi tên tệp di chuyển chứa dấu thời gian cho phép Laravel xác định thứ tự di chuyểnCác tùy chọn
php artisan migrate:rollback
5 và php artisan migrate:rollback
6 cũng có thể được sử dụng để chỉ ra tên của bảng và liệu quá trình di chuyển có tạo ra một bảng mới hay không. Các tùy chọn này điền trước tệp sơ khai di chuyển đã tạo với bảng đã chỉ địnhpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
1Nếu bạn muốn chỉ định đường dẫn đầu ra tùy chỉnh cho quá trình di chuyển đã tạo, bạn có thể sử dụng tùy chọn
php artisan migrate:rollback
7 khi thực hiện lệnh php artisan migrate:rollback
3. Đường dẫn đã cho phải liên quan đến đường dẫn cơ sở của ứng dụng của bạnCơ cấu di cư
Một lớp di chuyển chứa hai phương thức.
php artisan migrate:rollback
9 và public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
50. Phương thức php artisan migrate:rollback
9 được sử dụng để thêm các bảng, cột hoặc chỉ mục mới vào cơ sở dữ liệu của bạn, trong khi phương thức public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
50 sẽ đảo ngược các hoạt động được thực hiện bởi phương thức php artisan migrate:rollback
9Trong cả hai phương thức này, bạn có thể sử dụng trình tạo lược đồ Laravel để tạo và sửa đổi các bảng một cách rõ ràng. Để tìm hiểu về tất cả các phương pháp có sẵn trên trình tạo
php artisan migrate:rollback
2,. Ví dụ: ví dụ di chuyển này tạo bảng public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
55php artisan make:migration update_name_column_in_contacts_table
1Di chuyển đang chạy
Để chạy tất cả các lần di chuyển chưa hoàn thành của bạn, hãy thực hiện lệnh
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
56 Artisan{note} Nếu bạn đang sử dụng máy ảo Homestead, bạn nên chạy lệnh này từ bên trong máy ảo của mình
Buộc di chuyển để chạy trong sản xuất
Một số thao tác di chuyển mang tính phá hoại, có nghĩa là chúng có thể khiến bạn mất dữ liệu. Để bảo vệ bạn khỏi việc chạy các lệnh này đối với cơ sở dữ liệu sản xuất của mình, bạn sẽ được nhắc xác nhận trước khi các lệnh được thực thi. Để buộc các lệnh chạy mà không có dấu nhắc, hãy sử dụng cờ
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
57Quay lại di chuyển
Để khôi phục hoạt động di chuyển mới nhất, bạn có thể sử dụng lệnh
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
58. Lệnh này khôi phục "đợt" di chuyển cuối cùng, có thể bao gồm nhiều tệp di chuyểnphp artisan migrate:rollback
Bạn có thể khôi phục một số lần di chuyển hạn chế bằng cách cung cấp tùy chọn
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
59 cho lệnh public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
58. Ví dụ: lệnh sau sẽ khôi phục năm lần di chuyển cuối cùngpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
5Lệnh
php artisan make:migration update_name_column_in_contacts_table
91 sẽ khôi phục tất cả các lần di chuyển ứng dụng của bạnPhục hồi và di chuyển trong một lệnh
Lệnh
php artisan make:migration update_name_column_in_contacts_table
92 sẽ khôi phục tất cả các lần di chuyển của bạn và sau đó thực hiện lệnh public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
56. Lệnh này tạo lại toàn bộ cơ sở dữ liệu của bạn một cách hiệu quảphp artisan make:migration update_name_column_in_contacts_table
9Bạn có thể khôi phục và di chuyển lại một số lần di chuyển hạn chế bằng cách cung cấp tùy chọn
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
59 cho lệnh php artisan make:migration update_name_column_in_contacts_table
95. Ví dụ: lệnh sau sẽ khôi phục và di chuyển lại năm lần di chuyển cuối cùngpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
5Bỏ tất cả các bảng và di chuyển
Lệnh
php artisan make:migration update_name_column_in_contacts_table
96 sẽ loại bỏ tất cả các bảng khỏi cơ sở dữ liệu và sau đó thực hiện lệnh public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
56public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
8Những cái bàn
Tạo bảng
Để tạo một bảng cơ sở dữ liệu mới, hãy sử dụng phương thức
php artisan make:migration update_name_column_in_contacts_table
98 trên mặt tiền php artisan migrate:rollback
2. Phương thức php artisan make:migration update_name_column_in_contacts_table
98 chấp nhận hai đối số. Đầu tiên là tên của bảng, trong khi thứ hai là một public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
51 nhận một đối tượng public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
52 có thể được sử dụng để xác định bảng mớipublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
4Khi tạo bảng, bạn có thể sử dụng bất kỳ trình tạo lược đồ nào để xác định các cột của bảng
Kiểm tra sự tồn tại của bảng/cột
Bạn có thể dễ dàng kiểm tra sự tồn tại của một bảng hoặc cột bằng cách sử dụng phương thức
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
53 và public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
54public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
7Kết nối cơ sở dữ liệu & Tùy chọn bảng
Nếu bạn muốn thực hiện thao tác lược đồ trên kết nối cơ sở dữ liệu không phải là kết nối mặc định của mình, hãy sử dụng phương thức
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
55public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
10Bạn có thể sử dụng các lệnh sau trên trình tạo lược đồ để xác định các tùy chọn của bảng
LệnhMô tảpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
56Chỉ định công cụ lưu trữ bảng [MySQL]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
57Chỉ định bộ ký tự mặc định cho bảng [MySQL]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
58Chỉ định đối chiếu mặc định cho bảng [MySQL]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
59Tạo bảng tạm thời [ngoại trừ SQL Server]Đổi tên/xóa bảng
Để đổi tên một bảng cơ sở dữ liệu hiện có, hãy sử dụng phương pháp
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
80public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
11Để xóa một bảng hiện có, bạn có thể sử dụng các phương thức
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
81 hoặc public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
82public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
12Đổi tên bảng bằng khóa ngoại
Trước khi đổi tên bảng, bạn nên xác minh rằng bất kỳ ràng buộc khóa ngoại nào trên bảng đều có tên rõ ràng trong tệp di chuyển của bạn thay vì để Laravel gán tên dựa trên quy ước. Mặt khác, tên ràng buộc khóa ngoại sẽ tham chiếu đến tên bảng cũ
Cột
Tạo cột
Phương pháp
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
83 trên mặt tiền php artisan migrate:rollback
2 có thể được sử dụng để cập nhật các bảng hiện có. Giống như phương thức php artisan make:migration update_name_column_in_contacts_table
98, phương thức public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
83 chấp nhận hai đối số. tên của bảng và một public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
51 nhận một phiên bản public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
52 mà bạn có thể sử dụng để thêm các cột vào bảngpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
13Các loại cột có sẵn
Trình tạo lược đồ chứa nhiều loại cột mà bạn có thể chỉ định khi tạo bảng của mình
CommandDescriptionpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
89Cột tương đương UNSIGNED BIGINT [khóa chính] tăng tự động. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
40BIGINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
41BLOB cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
42BOOLEAN cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
43Cột tương đương CHAR với độ dài tùy chọn. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
44DATE cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
45DATETIME cột tương đương. ________ 446 Cột tương đương DATETIME [với múi giờ]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
47DECIMAL cột tương đương với độ chính xác [tổng số] và tỷ lệ [chữ số thập phân]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
48Cột tương đương NHÂN ĐÔI với độ chính xác [tổng số] và tỷ lệ [chữ số thập phân]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
49ENUM cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
70FLOAT cột tương đương với độ chính xác [tổng chữ số] và tỷ lệ [chữ số thập phân]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
71Cột tương đương HÌNH HỌC. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
72GEOMETRYCOLLECTION cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
73Cột tương đương UNSIGNED INTEGER [khóa chính] tự động tăng. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
74INTEGER cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
75Cột tương đương địa chỉ IP. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
76JSON cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
77JSONB cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
78LINESTRING cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
79LONGTEXT cột tương đương. Cột tương đương địa chỉ public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
100MAC. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
101Cột tương đương UNSIGNED MEDIUMINT [khóa chính] tự động tăng. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
102MEDIUMINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
103MEDIUMTEXT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
104Adds public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
105 UNSIGNED BIGINT và public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
106 cột tương đương VARCHAR. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
107MULTILINESTRING cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
108MULTIPOINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
109MULTIPOLYGON cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
110Thêm phiên bản nullable của cột public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
111. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
112Bí danh của phương thức public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
113. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
114POINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
115POLYGON cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
116Thêm cột tương đương public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
117 VARCHAR[100] nullable. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
118Cột tương đương UNSIGNED SMALLINT [khóa chính] tự động tăng. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
119SMALLINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
120Thêm một cột tương đương public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
121 DẤU THỜI GIAN có thể null để xóa mềm. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
122Thêm cột tương đương public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
121 DẤU THỜI GIAN [có múi giờ] không thể dùng để xóa mềm. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
124VARCHAR cột tương đương với độ dài tùy chọn. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
125Cột tương đương TEXT. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
126TIME cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
127TIME [với múi giờ] cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
128TIMESTAMP cột tương đương. ________ 1129 Cột tương đương DẤU THỜI GIAN [có múi giờ]. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
130Thêm các cột tương đương với dấu thời gian của public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
131 và public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
132 nullable. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
133Thêm các cột tương đương public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
131 và public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
132 DẤU THỜI GIAN [với múi giờ] có thể vô hiệu hóa. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
136Cột tương đương UNSIGNED TINYINT [khóa chính] tự động tăng. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
137TINYINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
138UNSIGNED BIGINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
139Cột tương đương DECIMAL UNSIGNED với độ chính xác [tổng chữ số] và tỷ lệ [chữ số thập phân]. ________ 1140 Cột tương đương UNSIGNED INTEGER. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
141UNSIGNED MEDIUMINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
142UNSIGNED SMALLINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
143UNSIGNED TINYINT cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
144UUID cột tương đương. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
145YEAR cột tương đươngCông cụ sửa đổi cột
Ngoài các loại cột được liệt kê ở trên, có một số "công cụ sửa đổi" cột mà bạn có thể sử dụng khi thêm cột vào bảng cơ sở dữ liệu. Ví dụ: để tạo cột "nullable", bạn có thể sử dụng phương thức
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
146public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
14Dưới đây là danh sách tất cả các công cụ sửa đổi cột có sẵn. Danh sách này không bao gồm các
Công cụ sửa đổi Mô tảpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
147Đặt cột "sau" một cột khác [MySQL]public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
148Đặt cột INTEGER làm tăng tự động [khóa chính]public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
149Chỉ định bộ ký tự cho cột [MySQL]public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
150Chỉ định đối chiếu cho cột [MySQL/SQL Server]public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
151Thêm nhận xét vào cột Sửa đổi cột
điều kiện tiên quyết
Trước khi sửa đổi một cột, hãy đảm bảo thêm phần phụ thuộc
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
161 vào tệp public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
162 của bạn. Thư viện Doctrine DBAL được sử dụng để xác định trạng thái hiện tại của cột và tạo các truy vấn SQL cần thiết để thực hiện các điều chỉnh đã chỉ định cho cộtpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
15Cập nhật thuộc tính cột
Phương pháp
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
163 cho phép bạn sửa đổi một số loại cột hiện có thành loại mới hoặc sửa đổi các thuộc tính của cột. Ví dụ: bạn có thể muốn tăng kích thước của cột chuỗi. Để xem phương pháp public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
163 đang hoạt động, hãy tăng kích thước của cột public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
165 từ 25 lên 50public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
16Chúng tôi cũng có thể sửa đổi một cột thành nullable
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
17{note} Chỉ có thể "thay đổi" các loại cột sau. bigInteger, nhị phân, boolean, ngày, dateTime, dateTimeTz, thập phân, số nguyên, json, longText, mediumText, smallInteger, chuỗi, văn bản, thời gian, unsignedBigInteger, unsignedInteger và unsignedSmallInteger
Đổi tên cột
Để đổi tên một cột, bạn có thể sử dụng phương pháp
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
166 trên Schema builder. Trước khi đổi tên cột, hãy đảm bảo thêm phần phụ thuộc public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
161 vào tệp public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
162 của bạnpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
18{note} Đổi tên bất kỳ cột nào trong bảng cũng có cột loại
169 hiện không được hỗ trợpublic function up[] { Schema::create['contacts', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->string['mobile_no']; $table->boolean['status']; $table->timestamps[]; }]; }
thả cột
Để thả một cột, hãy sử dụng phương pháp
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
170 trên trình tạo Schema. Trước khi loại bỏ các cột khỏi cơ sở dữ liệu SQLite, bạn sẽ cần thêm phần phụ thuộc public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
161 vào tệp public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
162 của mình và chạy lệnh public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
173 trong thiết bị đầu cuối của bạn để cài đặt thư việnpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
19Bạn có thể xóa nhiều cột khỏi một bảng bằng cách chuyển một mảng tên cột cho phương thức
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
170php artisan make:migration update_name_column_in_contacts_table
10{note} Không hỗ trợ loại bỏ hoặc sửa đổi nhiều cột trong một lần di chuyển khi sử dụng cơ sở dữ liệu SQLite
Bí danh lệnh có sẵn
LệnhMô tảpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
175Thả cột public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
117. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
177Thả cột public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
121. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
179Bí danh của phương pháp public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
180. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
181Thả cột public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
131 và public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
132. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
184Bí danh của phương pháp public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
185chỉ mục
Tạo chỉ mục
Trình tạo lược đồ hỗ trợ một số loại chỉ mục. Trước tiên, hãy xem một ví dụ chỉ định các giá trị của một cột phải là duy nhất. Để tạo chỉ mục, chúng ta có thể xâu chuỗi phương thức
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
186 vào định nghĩa cộtphp artisan make:migration update_name_column_in_contacts_table
11Ngoài ra, bạn có thể tạo chỉ mục sau khi xác định cột. Ví dụ
Bạn thậm chí có thể chuyển một mảng các cột tới một phương thức chỉ mục để tạo chỉ mục tổng hợp [hoặc tổng hợp]
php artisan make:migration update_name_column_in_contacts_table
12Laravel sẽ tự động tạo một tên chỉ mục hợp lý, nhưng bạn có thể truyền đối số thứ hai cho phương thức để tự chỉ định tên đó
php artisan make:migration update_name_column_in_contacts_table
13Các loại chỉ mục có sẵn
Mỗi phương thức chỉ mục chấp nhận một đối số thứ hai tùy chọn để chỉ định tên của chỉ mục. Nếu bỏ qua, tên sẽ được lấy từ tên của bảng và [các] cột
LệnhMô tảpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
187Thêm khóa chính. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
188Thêm phím tổng hợp. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
189Thêm một chỉ mục duy nhất. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
190Thêm một chỉ mục đơn giản. public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
191Thêm chỉ mục không gian. [ngoại trừ SQLite]Độ dài chỉ mục & MySQL/MariaDB
Laravel sử dụng bộ ký tự
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
192 theo mặc định, bao gồm hỗ trợ lưu trữ "biểu tượng cảm xúc" trong cơ sở dữ liệu. Nếu bạn đang chạy phiên bản MySQL cũ hơn 5. 7. 7 hoặc MariaDB cũ hơn 10. 2. 2, bạn có thể cần định cấu hình thủ công độ dài chuỗi mặc định được tạo bởi quá trình di chuyển để MySQL tạo chỉ mục cho chúng. Bạn có thể định cấu hình điều này bằng cách gọi phương thức public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
193 trong public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
194 của bạnphp artisan make:migration update_name_column_in_contacts_table
14Ngoài ra, bạn có thể bật tùy chọn
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
195 cho cơ sở dữ liệu của mình. Tham khảo tài liệu về cơ sở dữ liệu của bạn để biết hướng dẫn về cách bật tùy chọn này đúng cáchĐổi tên chỉ mục
Để đổi tên một chỉ mục, bạn có thể sử dụng phương pháp
public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
196. Phương thức này chấp nhận tên chỉ mục hiện tại làm đối số đầu tiên và tên mong muốn làm đối số thứ haiphp artisan make:migration update_name_column_in_contacts_table
15Giảm chỉ số
Để loại bỏ một chỉ mục, bạn phải chỉ định tên của chỉ mục. Theo mặc định, Laravel tự động gán tên hợp lý cho các chỉ mục. Nối tên bảng, tên của cột được lập chỉ mục và loại chỉ mục. Dưới đây là một số ví dụ
CommandDescriptionpublic function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
197Xóa khóa chính khỏi bảng "người dùng". public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
198Xóa một chỉ mục duy nhất khỏi bảng "người dùng". public function up[]
{
Schema::create['contacts', function [Blueprint $table]
{
$table->id[];
$table->string['name'];
$table->string['mobile_no'];
$table->boolean['status'];
$table->timestamps[];
}];
}
199Xóa chỉ mục cơ bản khỏi bảng "geo". php artisan make:migration update_name_column_in_contacts_table
100Xóa chỉ mục không gian khỏi bảng "geo" [ngoại trừ SQLite]Nếu bạn chuyển một mảng các cột vào một phương thức bỏ chỉ mục, tên chỉ mục thông thường sẽ được tạo dựa trên tên bảng, cột và loại khóa
php artisan make:migration update_name_column_in_contacts_table
16Ràng buộc khóa ngoại
Laravel cũng cung cấp hỗ trợ để tạo các ràng buộc khóa ngoại, được sử dụng để buộc tính toàn vẹn tham chiếu ở cấp cơ sở dữ liệu. Ví dụ: hãy xác định cột
php artisan make:migration update_name_column_in_contacts_table
101 trên bảng php artisan make:migration update_name_column_in_contacts_table
102 tham chiếu cột php artisan make:migration update_name_column_in_contacts_table
103 trên bảng php artisan make:migration update_name_column_in_contacts_table
104php artisan make:migration update_name_column_in_contacts_table
17Bạn cũng có thể chỉ định hành động mong muốn cho các thuộc tính "khi xóa" và "khi cập nhật" của ràng buộc
php artisan make:migration update_name_column_in_contacts_table
18Để xóa khóa ngoại, bạn có thể sử dụng phương pháp
php artisan make:migration update_name_column_in_contacts_table
105. Các ràng buộc khóa ngoại sử dụng quy ước đặt tên giống như các chỉ mục. Vì vậy, chúng tôi sẽ nối tên bảng và các cột trong ràng buộc sau đó thêm hậu tố tên bằng "_foreign"php artisan make:migration update_name_column_in_contacts_table
19Hoặc, bạn có thể chuyển một giá trị mảng sẽ tự động sử dụng tên ràng buộc thông thường khi thả
php artisan migrate:rollback
0Bạn có thể bật hoặc tắt các ràng buộc khóa ngoại trong quá trình di chuyển của mình bằng cách sử dụng các phương pháp sau
php artisan migrate:rollback
1{note} Theo mặc định, SQLite vô hiệu hóa các ràng buộc khóa ngoài. Khi sử dụng SQLite, hãy đảm bảo cấu hình cơ sở dữ liệu của bạn trước khi thử tạo chúng trong quá trình di chuyển của bạn
Làm cách nào để chạy tệp di chuyển bảng đơn trong Laravel?
Làm cách nào để di chuyển một bảng trong laravel? .Di chuyển nghệ nhân php di chuyển –path=/database/migrations/fileName. phpRollback php nghệ nhân di chuyển. rollback –path=/database/migrations/fileName. phpLàm mới di chuyển thủ công php. refresh –path=/database/migrations/fileName. phpLàm cách nào để chạy di chuyển cụ thể trong Laravel?
Để chạy quá trình di chuyển cụ thể trong Laravel, bạn cần phải sử dụng tùy chọn --path với lệnh di chuyển nghệ nhân php . Hãy lấy một ví dụ đơn giản, chúng tôi có '2019_12_04_131405_create_payments_table. php' trong thư mục cơ sở dữ liệu/di chuyển và chúng tôi muốn chạy quá trình di chuyển này.Làm cách nào để khôi phục chỉ một bảng trong Laravel?
Theo mặc định, php artisan di chuyển. khôi phục sẽ khôi phục tất cả các lần di chuyển cơ sở dữ liệu của bạn. Bằng cách chỉ định --step=1 , bạn đang nói rằng bạn chỉ muốn khôi phục quá trình di chuyển cơ sở dữ liệu mới nhất. Ngoài ra, nếu bạn thay đổi số, e. g. vào --step=2 , bạn đang yêu cầu Laravel chỉ khôi phục hai lần di chuyển cuối cùng.Làm cách nào để làm mới quá trình di chuyển cụ thể trong Laravel?
Nếu bạn đã cố chạy migration#2 mà không chạy lần nào #1 thì sẽ bị lỗi. Khi bạn chạy di chuyển thủ công thì nó sẽ chỉ chạy các di chuyển chưa được áp dụng trừ khi bạn sử dụng migrate. làm mới trong trường hợp đó, nó sẽ đặt lại và chạy lại tất cả các lần di chuyển.