Chuyên mục
Xây dựng website tin tức với Laravel 8

Tạo migration

Phác hoạ Database Entity Relationship Diagram(ERD) sẽ được sử dụng trong dự án này.[Laravel8] Blog Diagram-[ERD] Entity Relationship Diagram - News Demo
Danh sách table mặc định Khi chúng ta download Source Code Laravel về máy tính thì sẽ có được danh sách các table mặc định như hình bên dưới: Blog Diagram-table users

1. Update Column cho table users

Dựa vào bảng vẽ phác hoạ về mối quan hệ giữa các table, ta thấy table users ngoài các column mặc định được cung cấp sẵn bởi Laravel thì còn có một số column mới như:

  • column birthday (kiểu dữ liệu date).
  • column gender (kiểu dữ liệu tinyInt).
  • column address (kiểu dữ liệu varchar(255).

Truy cập file migration (2014_10_12_000000_create_users_table.php) để gõ thêm định nghĩa cho các column mới.

$table->date(‘birthday’)->nullable();
$table->tinyInteger(‘gender’)->nullable();
$table->string(‘address’)->nullable();
 
Xem hình ảnh đính kèm.
Blog Diagram-table users update

2. Tạo migration cho table admins

Step 1: Gõ lệnh tạo migration

Gõ vào lệnh bên dưới để tạo ra 1 file migration cho table admins.

php artisan make:migration create_admins_table –create=admins

Blog Diagram-table admins

Step 2: Truy cập vào file migration đã tạo

Truy cập vào file migration vừa tạo ra và gõ vào định nghĩa các column tương ứng của table admins.

$table->string(‘name’);
$table->string(’email’);
$table->string(‘password’);Blog Diagram-table admins update

 

 

3. Tạo migration cho table categories

Step 1: Gõ lệnh để tạo migration

Gõ vào lệnh artisan để tạo ra file migration cho table categories.

php artisan make:migration create_categories_table –create=categories

Blog Diagram-table categories

Step 2: Truy cập vào file migration đã tạo

Truy cập vào file migration vừa tạo ra và gõ vào định nghĩa các column tương ứng của table categories.

$table->string(‘name’);
$table->timestamps();
 
// Set Foreign Key
$table->foreign(‘creator_id’)->references(‘id’)->on(‘admins’);
 

 

Blog Diagram-table categories update

4. Tạo migration cho table articles

Step 1: Gõ lệnh tạo migration

Gõ vào lệnh artisan để tạo ra file migration cho table articles.

php artisan make:migration create_articles_table –create=articles

Blog Diagram-table articles

Step 2: Truy cập vào file migration đã tạo

Truy cập vào file migration vừa tạo ra và gõ vào định nghĩa các column tương ứng của table articles.

$table->unsignedBigInteger(‘category_id’);
$table->unsignedBigInteger(‘creator_id’);
$table->string(‘name’);
$table->longText(‘content’);
$table->tinyInteger(‘status’)->default(1);
$table->string(‘thumbnail’)->nullable();
$table->json(‘images’)->nullable();
 
// Set Foreign Key
$table->foreign(‘category_id’)->references(‘id’)->on(‘categories’);
$table->foreign(‘creator_id’)->references(‘id’)->on(‘admins’);
 
 
Blog Diagram-table articles update

5. Tạo migration cho table comments

Step 1: Gõ lệnh tạo migration

Gõ vào lệnh artisan để tạo ra file migration cho table comments.

php artisan make:migration create_comments_table –create=comments

Blog Diagram-table comments

Step 2: Truy cập vào file migration đã tạo
Truy cập vào file migration vừa tạo ra và gõ vào định nghĩa các column tương ứng của table comments.
$table->unsignedBigInteger(‘user_id’)->nullable();
$table->unsignedBigInteger(‘article_id’);
$table->string(‘content’);
$table->tinyInteger(‘status’)->default(1);
// Set Foreign Key
$table->foreign(‘user_id’)->references(‘id’)->on(‘users’);
[Laravel8] Blog Diagram-table comments update

6. Tạo migration cho table tags

Step 1: Gõ lệnh tạo migration

Gõ vào lệnh artisan để tạo ra file migration cho table tags.

php artisan make:migration create_tags_table –create=tags

Blog Diagram-table tags

Step 2: Truy cập vào file migration đã tạo

Truy cập vào file migration vừa tạo ra và gõ vào định nghĩa các column tương ứng của table tags.

$table->unsignedBigInteger(‘creator_id’);
$table->string(‘name’);
 
// Set Foreign Key
$table->foreign(‘creator_id’)->references(‘id’)->on(‘admins’);

 

 

7. Tạo migration cho table article_tag

Step 1: Gõ lệnh tạo migration

Gõ vào lệnh artisan để tạo ra file migration cho table article_tag.

php artisan make:migration create_article_tag_table –create=article_tag

Blog Diagram-table article_tag

Step 2: Truy cập vào file migration đã tạo

Truy cập vào file migration vừa tạo ra và gõ vào định nghĩa các column tương ứng của table article_tag.

$table->unsignedBigInteger(‘article_id’);
$table->unsignedBigInteger(‘tag_id’);

// Set Foreign Key
$table->foreign(‘article_id’)->references(‘id’)->on(‘articles’);
$table->foreign(‘tag_id’)->references(‘id’)->on(‘tags’);

$table->primary([‘article_id’, ‘tag_id’]);

 

Blog Diagram-table article_tag update

8. Chạy lệnh để thực thi các file migration đã định nghĩa xong cấu trúc

Chạy lệnh bên dưới để sinh ra cấu trúc cho các table đã được định nghĩa trong các file migration.

php artisan migrate

Blog Diagram-run migrate

Check trong MySQL

Blog Diagram-mysql

 

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *