Migration | Laravel | Hướng dẫn học | Học web chuẩn

Tạo dữ liệu với Migration

  • Migration được đặt bên trong thư mục

    /database/migrations/

    .

  • Để Migration hoạt động, chúng ta cần kết nối với cơ sở dữ liệu, nếu chưa biết cách kết nối thì bạn xem phần Kết nối database.
  • Thao tác với Migration, trước tiên, mở cửa sổ lệnh cmd, di chuyển tới thư mục myproject bằng lệnh:
cd C:\xampp\htdocs\myproject\

Tạo một Migration

  • Tạo Migration bằng lệnh Artisan:
php artisan make:migration create_news_table
  • php artisan – Công cụ hỗ trợ viết command line tích hợp sẵn trong Laravel, sẽ còn gặp lại nhiều.
  • make:migration – Lệnh tạo Migration.
  • create_news_table – Tên Migration do mình tự đặt.

Dòng lệnh trên thực thi sẽ cho ta kết quả sau:

Tạo một Migration

  • Sau khi thực thi lệnh, chúng ta sẽ nhận được thông báo file Migration đã được tạo

    “Created Migration: 2019_03_01_031800_create_news_table”

    .

  • File Migration vừa tạo sẽ được lưu tại thư mục

    /database/migrations/

    , có nội dung như sau:

<?php

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

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('news');
    }
}
  • use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; là các khai báo sử dụng cần cho thao tác Migration.
  • Chúng ta cần quan tâm là 2 function up()down():

    • public function up() dùng để thêm, bớt, thay đổi, … nội dung bảng cơ sở dữ liệu – để thực thi ta cần sử dụng lệnh php artisan migrate
    • public function down() dùng phục hồi hay xóa bảng, … – để thực thi ta cần sử dụng lệnh php artisan migrate:rollback

Với nội dung trên, ta đã có thể tạo bảng news với 1 cột là id, để có thể nhiều cột hơn ta thêm trực tiếp vào bên trong function up():

<?php

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

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('news');
    }
}
  • Nếu kiểm tra trong phpMyAdmin ngay lúc này thì bạn sẽ thấy chưa có table nào được tạo.
  • Để cập nhật thông tin vào Database thì ta cần thực thi lệnh php artisan migrate
php artisan migrate
  • Khi chạy lệnh trên, nếu kết quả xuất hiện thông báo sau là thành công:

Tạo một Migration

  • Lúc này đã có 4 bảng được tạo trong Database myproject:

    Tạo một Migration

    • migrations: chứa dữ liệu của Migration, lưu trữ thông tin các bảng dữ liệu được tạo trong Migration.
    • news: đây là bảng chúng ta đã tạo.
    • password_resets: bảng reset password có sẵn trong thư mục /database/migrations/, đây là table tồn tại sẵn của Laravel
    • users: bảng user có sẵn trong thư mục /database/migrations/, đây là table tồn tại sẵn của Laravel
  • Table news khi này sẽ có cột với dữ liệu rỗng như sau:

Tạo một Migration