Làm cách nào để thêm cột vào bảng hiện có bằng cách sử dụng di chuyển trong laravel 8?
Laravel thêm cột vào bảng hiện có bằng cách sử dụng ví dụ di chuyển; Show
Đôi khi cần thêm các cột mới trong bảng cơ sở dữ liệu của bạn. Hôm nay chúng tôi sẽ chỉ cho bạn cách thêm cột hoặc cột mới vào bảng hiện có bằng cách sử dụng di chuyển Laravel Laravel Thêm cột vào bảng hiện có bằng cách sử dụng MigrationThực hiện theo các bước bên dưới để thêm cột vào bảng hiện có bằng cách sử dụng di chuyển trong ứng dụng laravel
Hãy có một bảng được gọi là ghi chú trong đó bảng bạn muốn thêm loại ghi chú Thêm một Cột vào một bảng hiện cóĐể thêm một cột mới vào bảng hiện có bằng cách sử dụng di chuyển laravel. Hãy mở terminal của bạn và tạo tệp di chuyển bằng lệnh bên dưới php artisan make:migration add_type_to_notes Nó sẽ tạo một tệp add_type_to_notes trong thư mục di chuyển. Nó nên như thế này- string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } } Chạy lệnh di chuyểnChạy lệnh dưới đây trong thiết bị đầu cuối của bạn. Nó sẽ thêm một cột mới vào bảng của bạn php artisan migrate Bây giờ bạn có thể vào bảng cơ sở dữ liệu của mình và xem có một cột mới trong bảng của bạn. Nó đã được thêm thành công bằng cách sử dụng lệnh di chuyển Thêm nhiều cột trong bảng hiện cóNếu bạn cần thêm nhiều cột, bạn cần thêm lệnh sau vào terminal php artisan make:migration add_multiple_column_to_notes Lệnh trên sẽ tạo một tệp di chuyển, vì vậy hãy chuyển đến thư mục cơ sở dữ liệu/di chuyển và mở tệp của bạn và thêm cột theo bạn string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } } Cuối cùng, hãy chạy lệnh dưới đây để thêm các cột vào bảng cơ sở dữ liệu của bạn php artisan migrate Các loại cột có sẵnTrình tạo lược đồ Laravel đã cung cấp nhiều loại cột mà bạn có thể chỉ định khi tạo bảng của mình CommandDescription_______56Tự động tăng cột tương đương UNSIGNED BIGINT (khóa chính).php artisan migrate7BIGINT cột tương đương. php artisan migrate8BLOB cột tương đương. php artisan migrate9BOOLESE cột tương đương. Cột tương đương string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }0CHAR với độ dài tùy chọn. string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }1DATE cột tương đương. string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }2DATETIME cột tương đương. string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }3DATETIME (với múi giờ) cột tương đương. string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }4DECIMAL cột tương đương với độ chính xác (tổng chữ số) và tỷ lệ (chữ số thập phân). string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }5Cộ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). string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }6ENUM cột tương đương. string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }7FLOAT cột tương đương với độ chính xác (tổng số) và tỷ lệ (chữ số thập phân). string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }8Cột tương đương HÌNH HỌC. string('note_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['note_type']); }); } }9GEOMETRYCOLLECTION cột tương đương. php artisan migrate0Cột tương đương UNSIGNED INTEGER (khóa chính) tự động tăng. php artisan migrate1INTEGER cột tương đương. php artisan migrate2Cột tương đương địa chỉ IP. php artisan migrate3JSON cột tương đương. php artisan migrate4JSONB cột tương đương. php artisan migrate5LINESTRING cột tương đương. php artisan migrate6LONGTEXT cột tương đương. php artisan migrate7Cột tương đương địa chỉ MAC. php artisan migrate8Cột tương đương UNSIGNED MEDIUMINT (khóa chính) tự động tăng. php artisan migrate9MEDIUMINT cột tương đương. php artisan make:migration add_multiple_column_to_notes0MEDIUMTEXT cột tương đương. php artisan make:migration add_multiple_column_to_notes1Thêm php artisan make:migration add_multiple_column_to_notes2 UNSIGNED BIGINT và php artisan make:migration add_multiple_column_to_notes3 cột tương đương VARCHAR. php artisan make:migration add_multiple_column_to_notes4Thêm php artisan make:migration add_multiple_column_to_notes2 CHAR(36) và php artisan make:migration add_multiple_column_to_notes3 VARCHAR(255) cột tương đương UUID. php artisan make:migration add_multiple_column_to_notes7MULTILINESTRING cột tương đương. php artisan make:migration add_multiple_column_to_notes8MULTIPOINT cột tương đương. php artisan make:migration add_multiple_column_to_notes9MULTIPOLYGON cột tương đương. string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }0Thêm phiên bản nullable của string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }1 cột. string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }2Thêm phiên bản nullable của string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }3 cột. string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }4Bí danh của string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }5 phương pháp. string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }6POINT cột tương đương. string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }7POLYGON cột tương đương. string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }8Thêm cột tương đương string('short_description')->nullable(); $table->string('type')->nullable(); $table->string('long_description')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('notes', function (Blueprint $table) { $table->dropColumn(['short_description', 'type', 'long_description']); }); } }9 VARCHAR(100) nullable. php artisan migrate0SET cột tương đương. php artisan migrate1Cột tương đương UNSIGNED SMALLINT (khóa chính) tự động tăng. php artisan migrate2Cột tương đương SMALLINT. php artisan migrate3Thêm cột tương đương php artisan migrate4 DẤU THỜI GIAN để xóa mềm. php artisan migrate5Thêm một cột tương đương php artisan migrate4 DẤU THỜI GIAN (với múi giờ) có thể null để xóa mềm. php artisan migrate7Cột tương đương VARCHAR với độ dài tùy chọn. php artisan migrate8Cột tương đương TEXT. php artisan migrate9TIME cột tương đương. Cột tương đương php artisan migrate60TIME (với múi giờ). php artisan migrate61TIMESTAMP cột tương đương. php artisan migrate62TIMESTAMP (với múi giờ) cột tương đương. php artisan migrate63Thêm cột tương đương nullable php artisan migrate64 và php artisan migrate65 DẤU THỜI GIAN. php artisan migrate66Thêm cột tương đương nullable php artisan migrate64 và php artisan migrate65 DẤU THỜI GIAN (có múi giờ). php artisan migrate69Cột tương đương UNSIGNED TINYINT (khóa chính) tự động tăng. php artisan migrate70TINYINT cột tương đương. php artisan migrate71UNSIGNED BIGINT cột tương đương. php artisan migrate72Cột tương đương DECIMAL UNSIGNED với độ chính xác (tổng số) và tỷ lệ (chữ số thập phân). php artisan migrate73Cột tương đương UNSIGNED INTEGER. php artisan migrate74UNSIGNED MEDIUMINT cột tương đương. php artisan migrate75UNSIGNED SMALLINT cột tương đương. php artisan migrate76Cột tương đương TINYINT KHÔNG ĐƯỢC KÝ. php artisan migrate77UUID cột tương đương. cột tương đương php artisan migrate78YEAR Phần kết luậnHướng dẫn ví dụ về Laravel Migration, trong hướng dẫn này, bạn đã học cách thêm một hoặc nhiều cột vào các bảng hiện có trong cơ sở dữ liệu của mình bằng ví dụ Làm cách nào để thêm cột sau cột cụ thể trong quá trình di chuyển Laravel?Laravel cung cấp phương thức after() dùng để đặt một cột mới sau một cột khác. Hãy tạo một di chuyển mới cho bảng người dùng và sau đó chúng tôi sẽ thêm một cột mới vào bảng mới được tạo để hiểu rõ hơn. Lệnh trên sẽ tạo một di chuyển mới trong thư mục cơ sở dữ liệu/di chuyển.
Làm cách nào để cập nhật bảng di chuyển trong Laravel 8?Làm mới di chuyển
. Navigate to database/migrations/ folder from the project root. Mở tệp di chuyển bảng mà bạn muốn chỉnh sửa. Ở đây, tôi đang mở di chuyển bảng nhân viên. Cập nhật cấu trúc bảng trong phương thức up().
Làm cách nào để thay đổi bảng trong Laravel?Để làm điều này, bạn nên làm theo các bước sau. . tạo một tệp di chuyển mới php artisan make. cập nhật di chuyển_votes_table mở tệp di chuyển mới được tạo (app_folder\database\migrations{date_migrationfile_was_created}-update_votes_tables. php) thay đổi các cột bạn muốn thay đổi Làm cách nào để thêm nhiều cột trong Laravel?Sắp có Laravel vào tuần tới. Thêm nhiều cột sau một cột cụ thể. $table->string('address_line1')->after('password'); $table->string('address_line2')->after |