Laravel thêm cột duy nhất vào bảng hiện có

Laravel thêm cột duy nhất vào bảng hiện có

«

Trước đó

1

/

1

Kế tiếp

»

Laravel thêm cột duy nhất vào bảng hiện có

Laravel thêm cột duy nhất vào bảng hiện có

ấu trùng 5. 5 Hướng dẫn CRUD, Giới thiệu và Tổng quan về Chương trình nghị sự, Phần đào tạo Laravel – 1 DevopsSchool

«

Trước đó

1

/

1

Kế tiếp

»

Laravel thêm cột duy nhất vào bảng hiện có

Laravel thêm cột duy nhất vào bảng hiện có

Deepak Kumar

Kỹ sư phần mềm tại Cotocus Limited

Kỹ sư phần mềm toàn diện
Email - contact@DevOpsSchool. com

Laravel thêm cột duy nhất vào bảng hiện có

Bài đăng mới nhất của Deepak Kumar (xem tất cả)

  • Lỗi NPM. Node Sass chưa hỗ trợ môi trường hiện tại của bạn - Ngày 10 tháng 11 năm 2020
  • LỖI. sh. 1. chéo env. Quyền bị từ chối - Ngày 5 tháng 11 năm 2020
  • Lỗi phiên bản gói Vue không khớp với laravel - ngày 15 tháng 9 năm 2020

Đôi khi cần phải thêm các cột mới trong bảng Laravel. Hôm nay 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ó trong Laravel migration

Hãy tưởng tượng rằng, bạn có một bảng tên là người dùng với cấu trúc bảng như thế này

Laravel thêm cột duy nhất vào bảng hiện có

Sau khi di chuyển và có dữ liệu vào bảng, bây giờ có thể bạn muốn thêm một cột mới vào bảng người dùng có tên là profile. Để thêm một cột mới, làm mới cơ sở dữ liệu không phải là một cách lý tưởng. Trong tình huống này, bạn có thể tạo một tệp di chuyển mới để thêm một cột mới vào bảng người dùng

Thêm một cột

Để thêm một cột mới vào bảng hiện có trong Laravel, bạn cần thêm lệnh sau vào terminal

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 nào tùy thích

php artisan make:migration create_tests_table 

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

public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('age');
            $table->timestamps();
        });
    }

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

php artisan migrate

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

Laravel thêm cột duy nhất vào bảng hiện có

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

  • public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->integer('age');
                $table->timestamps();
            });
        }
    
    0. Thao tác này sẽ khôi phục đợt di chuyển cuối cùng

  • public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->integer('age');
                $table->timestamps();
            });
        }
    
    1. Đ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

  • public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->integer('age');
                $table->timestamps();
            });
        }
    
    2. 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
    public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->integer('age');
                $table->timestamps();
            });
        }
    
    3. Nó giống như tạo lại toàn bộ cơ sở dữ liệu của bạn

  • public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->integer('age');
                $table->timestamps();
            });
        }
    
    4. Thao tác này loại bỏ tất cả các bảng và thực hiện lại lệnh
    public function up()
        {
            Schema::create('tests', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->integer('age');
                $table->timestamps();
            });
        }
    
    3

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ó

Cột

public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('age');
            $table->timestamps();
        });
    }
7 đượ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

public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('age');
            $table->timestamps();
        });
    }
9

  • Sử dụng
    php artisan migrate
    
    0 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

 public function up()
    {
        Schema::table('tests', function (Blueprint $table) {
            $table->string('gender');
        });
    }

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 do rollbacks

public function down()
    {
        Schema::table('tests', function (Blueprint $table) {
            $table->dropColumn('gender');
        });
    }

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
php artisan migrate
4

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

Laravel thêm cột duy nhất vào bảng hiện có

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

Đối với

php artisan migrate
5 được đặt sau
php artisan migrate
6, 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 sẽ như thế này

 public function up()
    {
        Schema::table('tests', function (Blueprint $table) {
            $table->string('gender')->after('name');
        });
    }

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

Laravel thêm cột duy nhất vào bảng hiện có

Đã 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

php artisan migrate
6 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

php artisan make:migration drop_gender_from_tests_table --table=tests

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ỏ

public function up()
    {
        Schema::table('tests', function (Blueprint $table) {
            $table->dropColumn('name');
        });
    }

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.
    php artisan migrate
    
    4

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

Laravel thêm cột duy nhất vào bảng hiện có

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 để thêm cột mới vào bảng hiện có trong quá trình di chuyển Laravel?

Các bước để thêm mộ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 để tạo một cột duy nhất trong Laravel?

Laravel sẽ tự động tạo một tên chỉ mục duy nhất dựa trên tên bảng, tên cột và loại chỉ mục (duy nhất) khi tạo chỉ mục. Để xóa một chỉ mục duy nhất, trước tiên bạn phải đặt tên cho nó. Tạo một chỉ mục duy nhất với tên_user_email_unique . users_email_unique - là tên của Chỉ mục duy nhất.

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