Điều khó chịu khi xóa dữ liệu khỏi cơ sở dữ liệu là nó sẽ biến mất vĩnh viễn. Chúng tôi thậm chí không thể xem dữ liệu để xem liệu chúng tôi có cần nó hay không vì nó đã biến mất. Nếu chúng tôi cần lấy lại dữ liệu, giải pháp duy nhất của chúng tôi là khôi phục bản sao lưu và hy vọng chúng tôi có bản sao lưu ngay trước khi nó bị xóa để giảm thiểu tổn thất
Rất may, Laravel cung cấp một tính năng tích hợp sẵn cho phép chúng ta gắn cờ các hàng cơ sở dữ liệu là đã xóa mà không thực sự xóa chúng khỏi cơ sở dữ liệu. Bài viết/video này thảo luận về cách sử dụng Soft Deletes trong Laravel
Tại sao chúng ta nên sử dụng xóa mềm?
Như chúng ta đã thảo luận trong phần “Dừng xóa dữ liệu”, khi chúng ta
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
6 một hàng từ cơ sở dữ liệu, nó sẽ biến mất vĩnh viễn mà không cần trải qua quá trình khôi phục cơ sở dữ liệu đầy đủ có thể gây đau đớn. Xóa mềm dữ liệu cho phép chúng tôi dễ dàng xem và khôi phục dữ liệu với công việc tối thiểu và có thể tiết kiệm rất nhiều thời gian khi dữ liệu vô tình bị xóaLaravel cung cấp hỗ trợ xóa mềm bằng cách sử dụng đặc điểm
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
7di cư
Phần đầu tiên của quy trình chúng ta cần giải quyết là thiết lập các bảng cơ sở dữ liệu để có cột
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8Thêm các cột xóa mềm vào một bảng mới
Hãy bắt đầu bằng cách tạo một mô hình mới để theo dõi một
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
9 trong một ứng dụng quản lý dự án. Chúng tôi sẽ tạo mô hình và di chuyển trong một bước để chúng tôi có thể lười biếng nhất có thể$ php artisan make:model -m Project
Model created successfully.
Created Migration: 2020_05_02_012758_create_projects_table
Khi chúng tôi mở di chuyển mới được tạo, chúng tôi sẽ thêm các dòng sau vào chức năng
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
0 của chúng tôi$table->string['name'];
$table->softDeletes[];
Nó trông như thế này
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
Lệnh gọi hàm
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
1 là thứ thiết lập bảng để cho phép đặc điểm class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8 hoạt động. Không có nó, chúng tôi sẽ gặp lỗi truy vấnBây giờ chúng tôi sẽ chạy di chuyển
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
Hãy xem bảng trông như thế nào bên trong MySQL
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
1Như chúng ta có thể thấy có một cột
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
3 trong định nghĩa bảng. Đây là những gì cuộc gọi $ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
1 được thêm vào bảng và đặc điểm của class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8 sẽ sử dụng để theo dõi nếu hàng đã bị xóaThêm các cột xóa mềm vào một bảng hiện có
Hãy xem cách chúng ta có thể thêm các cột xóa mềm vào một bảng hiện có. Chúng tôi sẽ tạo một di chuyển mới bằng cách sử dụng lệnh
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
6 để thêm các cột vào bảng $ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
7 vì ngoài hộp, Laravel không có cột này trên bảng $ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
7class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8Tiếp theo, chúng ta sẽ thay đổi quá trình di chuyển để nó vừa thêm các cột trong hàm
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
0 [$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
1] vừa xóa chúng trong hàm class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
11 [class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
12]class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
3Một lần nữa, chúng tôi sẽ chạy di chuyển
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
4Thiết lập mô hình để sử dụng xóa mềm
Bây giờ chúng ta đã thiết lập các bảng cơ sở dữ liệu, chúng ta có thể bắt đầu làm việc với các mô hình đã xóa mềm trong mã của mình. Bước đầu tiên là thêm đặc điểm
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
7 vào các mô hình. Dưới đây là một mô hình ví dụ mà chúng tôi đã thiết lập nó để sử dụng logic class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8Điều quan trọng cần lưu ý là mặc dù chúng tôi đã thêm cột
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8 vào mô hình của mình nhưng Laravel không tự động sử dụng nó cho đến khi chúng tôi thêm đặc điểm, vì vậy chúng tôi vẫn sẽ xóa dữ liệu mà không có nó.class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8Xóa một mô hình
Bây giờ mọi thứ đã được thiết lập, hãy kiểm tra xem điều gì sẽ xảy ra
Trước tiên, chúng tôi sẽ sử dụng tinker để tạo một
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
9 mớiclass CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
0Khi chúng tôi kiểm tra cơ sở dữ liệu, chúng tôi có thể thấy rằng nó đã được duy trì trong cơ sở dữ liệu và cột
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
3 được đặt thành class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
18 cho biết rằng nó chưa bị xóa$table->string['name'];
$table->softDeletes[];
0Bây giờ chúng ta sẽ xóa mô hình
$table->string['name'];
$table->softDeletes[];
1Và quay lại MySQL, chúng ta có thể thấy
$ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
3 không còn là class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
18, điều này cho biết nó đã bị xóa$table->string['name'];
$table->softDeletes[];
2Khôi phục một mô hình
Nếu chúng ta vô tình xóa mô hình, Laravel giúp dễ dàng khôi phục bản ghi bằng hàm
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
81$table->string['name'];
$table->softDeletes[];
3$table->string['name'];
$table->softDeletes[];
0Xóa một mô hình
Giả sử bạn gặp trường hợp ai đó vô tình nhập thông tin và chúng tôi cần xóa bản ghi khỏi cơ sở dữ liệu chứ không chỉ đánh dấu nó là đã xóa.
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8 cung cấp chức năng class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
83 sẽ làm việc đó$table->string['name'];
$table->softDeletes[];
5$table->string['name'];
$table->softDeletes[];
6Tìm một mô hình đã xóa
Điều gì xảy ra khi chúng ta xóa một mô hình và cần tìm nó sau?
Đầu tiên, hãy thiết lập một
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
9 mới và xóa nhẹ nó$table->string['name'];
$table->softDeletes[];
7Khi chúng tôi cố gắng sử dụng
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
85, chúng tôi sẽ nhận được một class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
86 vì đặc điểm của class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8 đang lọc chúng ra$table->string['name'];
$table->softDeletes[];
8Để giải quyết vấn đề này, chúng ta cần gọi hàm
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
88 trước khi gọi hàm class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
85$table->string['name'];
$table->softDeletes[];
9Tìm một mô hình đã xóa trong một mối quan hệ
Một phần khác trong mã của chúng ta mà chúng ta cần chú ý là khi chúng ta có một mô hình xác định mối quan hệ Eloquent với các mô hình
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
8Thông thường, chúng tôi sẽ xác định mối quan hệ của chúng tôi như vậy
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
0Nếu
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
31 được liên kết với class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
9 này bị xóa mềm và chúng tôi cố gắng truy cập nó thông qua mối quan hệ này, hàm sẽ trả về class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
18class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
1Giải pháp cho vấn đề này một lần nữa là sử dụng hàm
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
88 để nó trả về kết quảclass CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
2____03Xem các mô hình bị xóa từ bộ điều khiển
Một trong những phần khó chịu về đặc điểm xóa mềm là nếu người dùng cố gắng truy cập trang để tìm tài nguyên đã bị xóa, họ sẽ nhận được lỗi 404 và không phải thứ gì hữu ích như thông báo cho họ biết mục đã bị xóa. Để giải quyết vấn đề này, chúng ta có thể thêm một cuộc gọi tới
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
88 vào định nghĩa tuyến đường của mìnhclass CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
4Thêm class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
36
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
Ngoài ra, một trong những điều chúng tôi muốn làm cũng là theo dõi ai đã xóa mô hình. Điều này rất hữu ích vì sau đó chúng tôi không chỉ có thể cho mọi người biết thời điểm một thực thể bị xóa mà còn có thể cho họ biết ai đã làm việc đó. Nó giúp bạn dễ dàng tìm ra lý do tại sao nó bị xóa nếu không phải vậy
Chúng tôi làm điều này bằng cách thêm một cột
class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
36 và sau đó chúng tôi tạo hàm class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
38 của riêng mình để đặt cả hai cột $ php artisan migrate
Migrating: 2020_05_02_012758_create_projects_table
Migrated: 2020_05_02_012758_create_projects_table [0.01 seconds]
3 và class CreateProjectsTable extends Migration
{
public function up[]
{
Schema::create['projects', function [Blueprint $table] {
$table->id[];
$table->timestamps[];
$table->string['name'];
$table->softDeletes[];
}];
}
public function down[]
{
Schema::dropIfExists['projects'];
}
}
36 thành các giá trị chính xác và lưu kết quả