Làm thế nào để bạn xóa một bảng trong laravel?

Chúng ta có thể xóa bản ghi bằng cách sử dụng mặt tiền DB bằng phương thức xóa. Cú pháp của phương thức xóa được hiển thị trong bảng sau

Cú pháp xóa(string $query, array $bindings = array()) Tham số
  • $query(string) − truy vấn để thực thi trong cơ sở dữ liệu
  • $bindings(array) − các giá trị để liên kết với truy vấn
ReturnsintDescriptionChạy câu lệnh xóa đối với cơ sở dữ liệu

Thí dụ

Bước 1 - Thực hiện lệnh bên dưới để tạo bộ điều khiển có tên là StudDeleteController

php artisan make:controller StudDeleteController --plain

Bước 2 - Sau khi thực hiện thành công, bạn sẽ nhận được đầu ra sau -

Làm thế nào để bạn xóa một bảng trong laravel?

Bước 3 - Sao chép đoạn mã sau vào tệp

ứng dụng/Http/Bộ điều khiển/StudDeleteController. php

ứng dụng/Http/Bộ điều khiển/StudDeleteController. php

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }

Bước 4 - Tạo một tệp xem có tên

tài nguyên/lượt xem/stud_delete_view. lưỡi. php và sao chép đoạn mã sau vào tệp đó

tài nguyên/lượt xem/stud_delete_view. lưỡi. php

   
   
      View Student Records
   
   
   
      
         @foreach ($users as $user)
         
         @endforeach
      
         
            ID
            Name
            Edit
         
            {{ $user->id }}
            {{ $user->name }}
            Delete
         
   

Bước 5 - Thêm các dòng sau vào app/Http/routes. php

ứng dụng/Http/tuyến. php

Route::get('delete-records','StudDeleteController@index');
Route::get('delete/{id}','StudDeleteController@destroy');

Bước 6 −Đầu ra sẽ xuất hiện như trong hình dưới đây

Làm thế nào để bạn xóa một bảng trong laravel?

Bước 7 - Nhấp vào liên kết xóa để xóa bản ghi đó khỏi cơ sở dữ liệu. Bạn sẽ được chuyển hướng đến một trang nơi bạn sẽ thấy một thông báo như trong hình sau

Làm thế nào để bạn xóa một bảng trong laravel?

Bước 8 - Nhấp vào liên kết “Click Here” và bạn sẽ được chuyển hướng đến một trang nơi bạn sẽ thấy tất cả các bản ghi ngoại trừ bản ghi đã bị xóa

Di chuyển Laravel đơn giản cho phép bạn dễ dàng thực hiện một số hành động nhất định đối với cơ sở dữ liệu mà không cần đến trình quản lý cơ sở dữ liệu (ví dụ:. phpMyAdmin). Chúng cũng có thể đóng vai trò kiểm soát phiên bản cho cơ sở dữ liệu của bạn

Tệp di chuyển laravel mặc định đi kèm với định nghĩa lớp chứa cả phương thức

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
1 và phương thức
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
2. Phương thức
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
1 được chạy khi quá trình di chuyển thực thi để áp dụng các thay đổi cho cơ sở dữ liệu trong khi phương thức
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
2 được chạy để hoàn nguyên những thay đổi đó

P. S. Đảm bảo rằng bạn đã kết nối ứng dụng laravel của mình với cơ sở dữ liệu trước khi tiếp tục. Nếu bạn không chắc chắn cách thực hiện, đây là hướng dẫn đơn giản

Tạo Migration

Di chuyển có thể được tạo đơn giản bằng lệnh sau

P. S. Các tệp di chuyển nằm trong thư mục

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
5. Tên của bảng sẽ được tạo là
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
6, bạn có thể thay đổi nó thành bất kỳ tên ưa thích nào

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
0

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Laravel sẽ sử dụng tên của quá trình di chuyển để cố gắng đoán 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. Nếu Laravel có thể xác định tên bảng từ tên di chuyển, Laravel sẽ điền trước vào tệp di chuyển đã tạo với bảng đã chỉ định

Tệp di chuyển sẽ trông như thế này theo mặc định

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

P. S.

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
7 chỉ được sử dụng khi một bảng được tạo ban đầu. Một lỗi phổ biến là cố gắng sử dụng nó để thêm cột vào bảng hiện có

Bảng kiểm tra nên có hai cột, tên (chuỗi) và tuổi (số nguyên) sẽ được viết trong

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
8 như sau

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
4

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Di chuyển đang chạy

Để thực hiện di chuyển đến cơ sở dữ liệu, hãy chạy lệnh

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
9 này

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
6

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Lệnh này chạy tất cả các lần di chuyển chưa hoàn thành

P. S. Xác nhận cơ sở dữ liệu rằng nó đã được cập nhật với các cột và loại tương ứng của chúng

Làm thế nào để bạn xóa một bảng trong laravel?

Các lệnh di chuyển khác

  • $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    40. Thao tác này sẽ khôi phục đợt di chuyển cuối cùng

  • $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    41. Điều này khôi phục tất cả các lần di chuyển ứng dụng của bạn

  • $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    42. Thao tác này sẽ khôi phục tất cả các lần di chuyển của bạn và thực hiện lệnh
    $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    43. Nó giống như tạo lại toàn bộ cơ sở dữ liệu của bạn

  • $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    44. Thao tác này loại bỏ tất cả các bảng và thực hiện lại lệnh
    $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    43

P. S. Rollback luôn thực thi phương thức

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
2 tương ứng

Cập nhật bảng. Thêm cột vào bảng hiện có

Một cột

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
47 được thêm vào bảng
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
6 theo các bước sau

  • Tạo tệp di chuyển

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
49

  • Sử dụng
    $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    60 trong phương thức
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateTestsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('tests');
        }
    }
    
    1 sẽ được cung cấp theo mặc định, các cột có thể được thêm vào như sau

   
   
      View Student Records
   
   
   
      
         @foreach ($users as $user)
         
         @endforeach
      
         
            ID
            Name
            Edit
         
            {{ $user->id }}
            {{ $user->name }}
            Delete
         
   

9

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  • Thiết lập tùy chọn rollback

Phương pháp

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
2 cũng nên được cập nhật vì rollbacks

Route::get('delete-records','StudDeleteController@index');
Route::get('delete/{id}','StudDeleteController@destroy');
1

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  • Bây giờ thực hiện di chuyển

Để chạy di chuyển, hãy sử dụng lệnh

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
9 này.
______164.

P. S. Xác nhận rằng cột giới tính đã được thêm vào bảng kiểm tra trên cơ sở dữ liệu của bạn

Làm thế nào để bạn xóa một bảng trong laravel?

Ghi chú. Laravel đặt cột được thêm cuối cùng trên bảng, tuy nhiên nó có thể được đặt ở bất kỳ vị trí mong muốn nào trên bảng

Để

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
65 được đặt sau
$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
66, thì
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
8 thà như thế này

Route::get('delete-records','StudDeleteController@index');
Route::get('delete/{id}','StudDeleteController@destroy');
7

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Điều này có vẻ có tổ chức hơn và tốt hơn

Làm thế nào để bạn xóa một bảng trong laravel?

Đã thêm thành công cột giới tính vào bảng kiểm tra

Cập nhật bảng. Xóa cột khỏi bảng hiện có

Có một số cách để xóa một cột khỏi bảng

1. Xóa một cột

Để xóa cột

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here to go back.'; } }
66 khỏi bảng
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
6

  • Tạo tệp di chuyển bằng lệnh
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateTestsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('tests');
        }
    }
    
    9 này

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
1

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  • Cập nhật phương thức
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateTestsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('tests');
        }
    }
    
    1 với cột bạn muốn loại bỏ

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}
3

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  • Chạy di chuyển Thực hiện di chuyển bằng lệnh
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateTestsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('tests');
        }
    }
    
    9 này.
    $users]);
       }
       public function destroy($id) {
          DB::delete('delete from student where id = ?',[$id]);
          echo "Record deleted successfully.
    "; echo 'Click Here to go back.'; } }
    64

P. S. Xác nhận rằng cột tên đã bị loại bỏ trên bảng kiểm tra

Làm thế nào để bạn xóa một bảng trong laravel?

P. S. Tên tệp di chuyển là duy nhất nên mỗi tệp di chuyển phải có các tên khác nhau khi tạo chúng

Làm cách nào để xóa bảng trong laravel?

chỉ mục hàm công khai() { DB. bảng('người dùng')->xóa(); }

Làm cách nào để xóa bảng trong Laravel 9?

Làm cách nào để xóa một bảng cụ thể trong Laravel? .
1) Xóa bảng khỏi phpmyadmin. Bạn có thể xóa bảng trực tiếp từ phpmyadmin
2) Xóa các tệp di chuyển khỏi laravel. Trong phương pháp thứ hai thả tất cả các bảng từ cơ sở dữ liệu
3) Tạo một di chuyển mới để xóa bảng

Làm cách nào để bỏ bảng trong Laravel 8?

nếu bạn đã tạo bảng từ tệp di chuyển thì chỉ cần khôi phục quá trình di chuyển, hãy chạy lệnh này trong cli php artisan di chuyển. phục hồi. .
mình làm rồi mà lỗi vẫn vậy. .
hiển thị tệp di chuyển của bạn nơi bạn đã tạo bảng bài hát

Làm cách nào để xóa bảng trong PHP?

Xóa bảng bằng PHP Script . Hàm này nhận hai tham số và trả về TRUE nếu thành công hoặc FALSE nếu thất bại. PHP uses mysqli query() or mysql_query() function to drop a MySQL table. This function takes two parameters and returns TRUE on success or FALSE on failure.