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

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

Route::get('user-data', function() {

$model = App\User::query();

return Datatables::eloquent($model)

->removeColumn('password')

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_table
0 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_table
1

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ể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

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

  1. Cách chọn dữ liệu giữa hai ngày trong MySQL
  2. Lỗi sau khi cấu hình thủ công php. bộ đệm trong Laravel
  3. Khóa được chỉ định là lỗi quá dài trong Laravel
  4. Yêu cầu bài đăng AJAX PHP với ví dụ
  5. Cách sử dụng Laravel Soft Delete
  6. Làm thế nào để thêm phân trang Laravel Next Prev
  7. lỗi cURL 60. Sự cố chứng chỉ SSL. không thể lấy chứng chỉ nhà phát hành địa phương
  8. Sự khác biệt giữa Factory và Seeder trong Laravel
  9. ấu trùng. Tăng Số Lượng Nếu Sản Phẩm Đã Có Trong Giỏ Hàng
  10. Cách Tính Tuổi Từ Ngày Sinh
  11. Cách kiểm tra phiên bản Laravel PHP
  12. Cách xử lý các công việc thất bại trong Laravel
  13. Cách xóa dữ liệu WooC Commerce sau khi gỡ cài đặt
  14. Cách lấy bản ghi mới nhất trong Laravel
  15. Cách phá vỡ các vòng lặp lồng nhau trong PHP hoặc Laravel
  16. Cách truyền tham số URL của Laravel

Đó 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.