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;

Đô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 Migration

Thự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

  • Thêm một Cột vào một bảng hiện có
  • Thêm nhiều cột trong bảng hiện có
  • Các loại cột có sẵn

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

Chạ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ẵn

Trì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 migrate
7BIGINT cột tương đương.
php artisan migrate
8BLOB cột tương đương.
php artisan migrate
9BOOLESE 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 migrate
0Cột tương đương UNSIGNED INTEGER (khóa chính) tự động tăng.
php artisan migrate
1INTEGER cột tương đương.
php artisan migrate
2Cột tương đương địa chỉ IP.
php artisan migrate
3JSON cột tương đương.
php artisan migrate
4JSONB cột tương đương.
php artisan migrate
5LINESTRING cột tương đương.
php artisan migrate
6LONGTEXT cột tương đương.
php artisan migrate
7Cột tương đương địa chỉ MAC.
php artisan migrate
8Cột tương đương UNSIGNED MEDIUMINT (khóa chính) tự động tăng.
php artisan migrate
9MEDIUMINT cột tương đương.
php artisan make:migration add_multiple_column_to_notes
0MEDIUMTEXT cột tương đương.
php artisan make:migration add_multiple_column_to_notes
1Thêm 
php artisan make:migration add_multiple_column_to_notes
2 UNSIGNED BIGINT và 
php artisan make:migration add_multiple_column_to_notes
3 cột tương đương VARCHAR.
php artisan make:migration add_multiple_column_to_notes
4Thêm 
php artisan make:migration add_multiple_column_to_notes
2 CHAR(36) và 
php artisan make:migration add_multiple_column_to_notes
3 VARCHAR(255) cột tương đương UUID.
php artisan make:migration add_multiple_column_to_notes
7MULTILINESTRING cột tương đương.
php artisan make:migration add_multiple_column_to_notes
8MULTIPOINT cột tương đương.
php artisan make:migration add_multiple_column_to_notes
9MULTIPOLYGON 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 migrate
0SET cột tương đương.
php artisan migrate
1Cột tương đương UNSIGNED SMALLINT (khóa chính) tự động tăng.
php artisan migrate
2Cột tương đương SMALLINT.
php artisan migrate
3Thêm cột tương đương
php artisan migrate
4 DẤU THỜI GIAN để xóa mềm.
php artisan migrate
5Thêm một cột tương đương
php artisan migrate
4 DẤU THỜI GIAN (với múi giờ) có thể null để xóa mềm.
php artisan migrate
7Cột tương đương VARCHAR với độ dài tùy chọn.
php artisan migrate
8Cột tương đương TEXT.
php artisan migrate
9TIME cột tương đương. Cột tương đương
php artisan migrate
60TIME (với múi giờ).
php artisan migrate
61TIMESTAMP cột tương đương.
php artisan migrate
62TIMESTAMP (với múi giờ) cột tương đương.
php artisan migrate
63Thêm cột tương đương nullable 
php artisan migrate
64 và 
php artisan migrate
65 DẤU THỜI GIAN.
php artisan migrate
66Thêm cột tương đương nullable 
php artisan migrate
64 và 
php artisan migrate
65 DẤU THỜI GIAN (có múi giờ).
php artisan migrate
69Cột tương đương UNSIGNED TINYINT (khóa chính) tự động tăng.
php artisan migrate
70TINYINT cột tương đương.
php artisan migrate
71UNSIGNED BIGINT cột tương đương.
php artisan migrate
72Cộ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 migrate
73Cột tương đương UNSIGNED INTEGER.
php artisan migrate
74UNSIGNED MEDIUMINT cột tương đương.
php artisan migrate
75UNSIGNED SMALLINT cột tương đương.
php artisan migrate
76Cột tương đương TINYINT KHÔNG ĐƯỢC KÝ.
php artisan migrate
77UUID cột tương đương. cột tương đương
php artisan migrate
78YEAR

Phần kết luận

Hướ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