Trong Laravel, Migration cung cấp một cách để dễ dàng chia sẻ lược đồ của cơ sở dữ liệu. Nó cũng làm cho việc sửa đổi lược đồ dễ dàng hơn nhiều. Nó giống như tạo một lược đồ một lần và sau đó chia sẻ nó nhiều lần. Nó rất hữu ích khi bạn có nhiều bảng và cột vì nó sẽ giảm bớt công việc tạo bảng theo cách thủ công Show Để tạo di chuyển. Nó có thể được tạo bằng cách sử dụng lệnh artisan như hình bên dưới php artisan make:migration create_articles_table Ở đây, các bài báo sẽ là bảng và thay vào đó, bạn có thể viết bất kỳ tên bảng nào khác mà bạn muốn tạo và phù hợp với ứng dụng nhưng tên bảng như bạn đã thấy, phải ở dạng số nhiều. Vì vậy, nó được viết dưới dạng bài báo chứ không phải bài viết. Đây là sơ đồ đặt tên được Laravel sử dụng và điều quan trọng là phải chỉ định tạo ở đầu và bảng ở cuối theo sơ đồ đặt tên của tệp Di chuyển trong Laravel Tất cả tệp di chuyển mà chúng ta tạo bằng lệnh artisan đều nằm ở thư mục cơ sở dữ liệu/di chuyển. Vì vậy, sau khi chúng tôi chạy lệnh trên, nó sẽ tạo một tệp PHP với tên chúng tôi đã chỉ định với ngày và giờ hiện tại Và tệp sẽ được tạo với một số lớp và chức năng được xác định trước như trong mã bên dưới php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg9 php artisan make:migration create_articles_table --create=gfg0 php artisan make:migration create_articles_table --create=gfg1 php artisan make:migration create_articles_table --create=gfg0 php artisan make:migration create_articles_table --create=gfg3 php artisan make:migration create_articles_table --create=gfg0 php artisan make:migration create_articles_table --create=gfg1 php artisan make:migration create_articles_table --create=gfg9 php artisan make:migration create_articles_table --create=gfg3 php artisan make:migration create_articles_table --create=gfg4 php artisan make:migration create_articles_table --create=gfg5 php artisan make:migration create_articles_table --create=gfg6 php artisan make:migration create_articles_table --create=gfg7 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg9 php artisan migrate0 php artisan migrate1 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg7 php artisan migrate4 php artisan migrate5 php artisan migrate6 php artisan migrate7 php artisan migrate0 php artisan migrate9 php artisan make:migration create_articles_table --create=gfg90 php artisan make:migration create_articles_table --create=gfg91 php artisan make:migration create_articles_table --create=gfg92 php artisan make:migration create_articles_table --create=gfg90 php artisan make:migration create_articles_table --create=gfg94 php artisan make:migration create_articles_table --create=gfg95 php artisan make:migration create_articles_table --create=gfg96 php artisan make:migration create_articles_table --create=gfg92____190____199 php artisan migrate4 php artisan make:migration create_articles_table --create=gfg81 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg83 php artisan make:migration create_articles_table --create=gfg9 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg9 php artisan migrate0 php artisan make:migration create_articles_table --create=gfg88 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg7 php artisan migrate4 php artisan make:migration create_articles_table --create=gfg92 php artisan migrate6 php artisan make:migration create_articles_table --create=gfg96 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg83 php artisan make:migration create_articles_table --create=gfg83 Nếu bạn muốn chỉ định tên của bảng khác với tên mà bạn đã chỉ định làm tên tệp thì bạn có thể sử dụng một tùy chọn là –create với lệnh như sau php artisan make:migration create_articles_table --create=gfg Với lệnh này, tên bảng chứa trong phương thức create() sẽ là gfg chứ không phải các bài viết, được chỉ định trong tên của tệp php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg9 php artisan make:migration create_articles_table --create=gfg0 php artisan make:migration create_articles_table --create=gfg1 php artisan make:migration create_articles_table --create=gfg0 php artisan make:migration create_articles_table --create=gfg3 php artisan make:migration create_articles_table --create=gfg0 php artisan make:migration create_articles_table --create=gfg1 php artisan make:migration create_articles_table --create=gfg9 php artisan make:migration create_articles_table --create=gfg3 php artisan make:migration create_articles_table --create=gfg4 php artisan make:migration create_articles_table --create=gfg5 php artisan make:migration create_articles_table --create=gfg6 php artisan make:migration create_articles_table --create=gfg7 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg9 php artisan migrate0 php artisan migrate1 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg7 php artisan migrate4 php artisan migrate5 php artisan make:migration create_articles_table --create=gfg00 php artisan migrate7 php artisan migrate0 php artisan migrate9 php artisan make:migration create_articles_table --create=gfg90 php artisan make:migration create_articles_table --create=gfg91 php artisan make:migration create_articles_table --create=gfg92 php artisan make:migration create_articles_table --create=gfg90 php artisan make:migration create_articles_table --create=gfg94 php artisan make:migration create_articles_table --create=gfg95 php artisan make:migration create_articles_table --create=gfg96 php artisan make:migration create_articles_table --create=gfg92____190____199 php artisan migrate4 php artisan make:migration create_articles_table --create=gfg81 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg83 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg9 php artisan migrate0 php artisan make:migration create_articles_table --create=gfg88 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg7 _______94____792____800 php artisan make:migration create_articles_table --create=gfg96 php artisan make:migration create_articles_table --create=gfg8 php artisan make:migration create_articles_table --create=gfg83 php artisan make:migration create_articles_table --create=gfg83 Cấu trúc cơ bản của một Migration. Tệp di chuyển chứa một lớp có tên của tệp được chỉ định trong khi tạo quá trình di chuyển và nó mở rộng Di chuyển. Trong đó chúng ta có hai hàm, hàm đầu tiên là hàm up() và hàm thứ hai là hàm down(). Hàm up() được gọi khi chúng ta chạy di chuyển để tạo một bảng và các cột được chỉ định và hàm ‘down()’ được gọi khi chúng ta muốn hoàn tác việc tạo hàm ‘up()’ Trong hàm up(), chúng ta có phương thức tạo của Schema facade (trình tạo lược đồ) và như chúng ta đã nói trước đây, đối số đầu tiên trong hàm này là tên của bảng sẽ được tạo. Đối số thứ hai là một hàm có đối tượng Blueprint làm tham số và để xác định bảng Trong hàm down(), chúng ta có một phương thức dropIfExists của trình tạo lược đồ mà khi được gọi sẽ loại bỏ bảng Để chạy Migration. Trước khi chạy di chuyển, trước tiên chúng ta phải tạo Cơ sở dữ liệu MySQL và Kết nối với nó. Sau khi xong, để Run Migration, chúng ta có thể sử dụng lệnh Artisan như sau Mục đích của việc di chuyển Laravel là gì?Laravel Migrations cho phép các nhà phát triển lập trình tạo, cập nhật và hủy các bảng cơ sở dữ liệu , hoạt động như một hệ thống kiểm soát phiên bản cho lược đồ cơ sở dữ liệu của bạn.
Việc di chuyển cơ sở dữ liệu có cần thiết không?Việc di chuyển DB là cần thiết vì nó giúp tiết kiệm tiền . Lợi ích của việc di chuyển cơ sở dữ liệu là nó giúp di chuyển dữ liệu từ một hệ thống cũ đã lỗi thời sang một phần mềm hiện đại hóa. Di chuyển cơ sở dữ liệu giúp thống nhất dữ liệu khác nhau, do đó, có thể truy cập dữ liệu bằng các kỹ thuật khác nhau.
Tôi có nên sử dụng di chuyển?Việc di chuyển rất hữu ích vì chúng cho phép lược đồ cơ sở dữ liệu phát triển khi các yêu cầu thay đổi . Chúng giúp các nhà phát triển lập kế hoạch, xác thực và áp dụng các thay đổi giản đồ một cách an toàn cho môi trường của họ.
Khi nào bạn nên chạy di chuyển?Chạy di chuyển cơ sở dữ liệu trước, trước khi bạn triển khai mã mới . Điều này có nghĩa là mã trước phải hoạt động với cả hai lược đồ cơ sở dữ liệu, nhưng mã sau có thể cho rằng các bảng đã được thêm vào. |