Laravel xóa cột khỏi truy vấn
CẢNH BÁO Bạn đang xem tài liệu về phiên bản cũ của LARAVEL-DATATABLES. Cân nhắc nâng cấp dự án của bạn lên laravel-datatables 10. 0 Show Bạn có thể xóa một cột trong phản hồi của mình bằng cách sử dụng api id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }7 Trong bài viết này, chúng ta sẽ tìm hiểu về laravel xóa các cột khỏi bảng trong Laravel migration. Di chuyển Laravel rất hữu ích để xử lý cơ sở dữ liệu bằng các lệnh đơn giản Ngoài ra, rất tiện lợi trong giai đoạn phát triển để tạo dữ liệu giả bằng cách sử dụng Factory và Seeders. Hơn nữa, bạn có thể dễ dàng di chuyển cơ sở dữ liệu của mình bằng một lệnh trong quá trình sản xuất. Vì vậy, chúng ta hãy nhảy vào nó ấu trùng. Xóa cột khỏi bảng khi di chuyểnĐể xóa một cột khỏi bảng trong quá trình di chuyển, Laravel sử dụng phương thức. Phương thức này chỉ chấp nhận 1 đối số và đó là tên cột id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }8 hoặc tên cột id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }9 ghi chú. Nếu bạn đang sử dụng cơ sở dữ liệu SQLite thì bạn phải cài đặt gói php artisan make:migration create_users_table0 thông qua trình quản lý gói Composer trước khi có thể sử dụng phương thức php artisan make:migration create_users_table1 Hãy lấy một ví dụ đơn giản để hiểu rõ hơn. Giả sử chúng ta có bảng id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }0 và chúng ta có quá trình di chuyển sau php artisan make:migration create_users_table id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } Sau khi di chuyển cơ sở dữ liệu, bây giờ chúng tôi muốn xóa 2 cột id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }1 và id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }2. Hãy tạo di chuyển mới để cập nhật bảng id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }3 bằng cách chạy lệnh sau php artisan make:migration update_users_table Ví dụ 01 Xóa cột khỏi bảng bằng di chuyển________số 8 Phương thức di chuyển id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }4 sẽ thực thi khi chúng ta chạy lệnh id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }5 và phương thức id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }6 sẽ thực thi khi chúng ta khôi phục quá trình di chuyển bằng cách sử dụng id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }7 Bây giờ, hãy xóa nhiều cột chỉ bằng một phương thức id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }8 Ví dụ 02 Xóa nhiều cột khỏi bảng bằng di chuyểnid(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }4 Ví dụ 03 Chỉ xóa nếu các cột tồn tại khỏi bảng bằng cách sử dụng di chuyểnĐôi khi, trong khi di chuyển cơ sở dữ liệu và khôi phục có thể tạo ra các lỗi lạ. Nó có thể nói rằng cột không tồn tại hoặc đã tồn tại thì bạn có thể xem ví dụ này Laravel cung cấp hàm id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }9 để kiểm tra cột có tồn tại trong một bảng nhất định hay không. Hãy xem một ví dụ id(); $table->uuid('uuid')->unique(); $table->text('title')->nullable(); $table->text('first_name'); $table->text('middle_name')->nullable(); $table->text('last_name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->text('gender')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }6 Ngoài ra, hãy đọc hướng dẫn của chúng tôi
Đó là nó từ cuối của chúng tôi. Chúng tôi hy vọng bài viết này đã giúp bạn xóa một cột khỏi bảng trong quá trình di chuyển Laravel Vui lòng cho chúng tôi biết trong phần nhận xét nếu mọi thứ hoạt động như mong đợi, các vấn đề của bạn hoặc bất kỳ câu hỏi nào. Nếu bạn nghĩ rằng bài viết này đã tiết kiệm thời gian và tiền bạc của bạn, vui lòng bình luận, chia sẻ, thích và đăng ký. Cảm ơn bạn đã đọc bài đăng này 🙂 Hãy mỉm cười. mã hóa hạnh phúc Làm cách nào để xóa một cột trong Laravel?Xóa cột bằng Di chuyển. sử dụng Chiếu sáng\Hỗ trợ\Mặt tiền\Lược đồ; . Xóa nhiều cột bằng cách sử dụng di chuyển. sử dụng Chiếu sáng\Hỗ trợ\Mặt tiền\Lược đồ; . Xóa cột nếu tồn tại bằng cách sử dụng Di chuyển Làm cách nào để xóa di chuyển trong Laravel?Trong Laravel hình như có lệnh tạo migration nhưng không xóa. . Xóa thủ công tệp di chuyển trong ứng dụng/cơ sở dữ liệu/di chuyển/my_migration_file_name. php Đặt lại tệp tự động tải của nhà soạn nhạc. nhà soạn nhạc dump-autoload Sửa đổi cơ sở dữ liệu của bạn. Xóa mục nhập cuối cùng khỏi bảng di chuyển Làm cách nào để thêm cột mới vào bảng bằng cách sử dụng di chuyển trong Laravel 8?Các bước để thêm một cột hoặc nhiều cột vào bảng hiện có bằng cách sử dụng di chuyển trong laravel. . Thêm tên cột vào trường. . Để 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. . Thêm mã bên dưới vào tệp di chuyển mới được thêm vào. . Chạy lệnh di chuyển Làm cách nào để khôi phục quá trình di chuyển 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. |