Trong Laravel Framework, bạn có thể dùng Artisan Command để nhanh chóng tạo ra Model, Controller và Migration mà không cần viết thủ công từng file.
Điều này giúp tiết kiệm thời gian và đảm bảo cấu trúc tuân theo chuẩn của Laravel.
Cú pháp:
php artisan make:model TênModel
Ví dụ:
php artisan make:model Product
Kết quả:
app/Models/Product.php
Tùy chọn mở rộng:
-m
: tạo migration kèm theo model-c
: tạo controller kèm theo model-r
: tạo controller resource (có sẵn 7 method CRUD)-f
: tạo factory để fake dữ liệu-s
: tạo seeder để insert dữ liệu mẫuVí dụ đầy đủ:
php artisan make:model Product -mcrfs
=> Tạo Model Product
, Migration, Controller resource, Factory, Seeder.
Cú pháp:
php artisan make:controller TênController
Ví dụ:
php artisan make:controller ProductController
Kết quả:
app/Http/Controllers/ProductController.php
Tùy chọn:
--resource
hoặc -r
: tạo controller CRUD đầy đủ (index
, create
, store
, show
, edit
, update
, destroy
)--model=ModelName
: gắn trực tiếp model vào controllerVí dụ:
php artisan make:controller ProductController -r --model=Product
=> Tạo controller resource, tự động import App\Models\Product
.
Cú pháp:
php artisan make:migration tên_bảng
Ví dụ:
php artisan make:migration create_products_table
Kết quả:
database/migrations/
Một số tùy chọn:
--create=table_name
: tạo migration kèm hàm Schema::create
--table=table_name
: tạo migration kèm hàm Schema::table
(để sửa bảng)Ví dụ:
php artisan make:migration add_price_to_products_table --table=products
=> Migration thêm cột price
vào bảng products
.
Laravel cho phép bạn gộp trong một lệnh duy nhất:
php artisan make:model Product -mcr
-m
→ tạo migration-c
→ tạo controller-r
→ controller dạng resource CRUDSau khi tạo migration, chạy:
php artisan migrate
=> Laravel sẽ tạo hoặc cập nhật bảng trong database.
✅ Tóm lại:
make:model
→ Tạo Model (có thể kèm migration, controller, factory, seeder)make:controller
→ Tạo Controller (có thể kèm resource, gắn model)make:migration
→ Tạo file migration (có thể tạo bảng mới hoặc chỉnh sửa bảng)php artisan make:model TenModel -mcr
để tạo đủ Model + Migration + Controller resource trong 1 lệnh.php artisan make:model Product -m
php artisan make:controller ProductController --model=Product
Mở cmd và chạy lệnh
php artisan migrate
- Lệnh trên sẽ tìm và áp dụng tất cả các file migratiion trong thư mục
database\migrations
và tạo cấu trúc các bảng theo thông tin khai báo trong các file đó- Nó sẽ chạy hàm up trong file migration
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id(); // id bigint unsigned primary key auto_increment
$table->string('name'); //name varchar(255) NOT NULL
$table->tinyInteger('status')->nullable()->default(1); /// status tinyint null default(1)
$table->timestamps(); // created_at, updated_at
});
}
php artisan migrate:rollback
Lệnh trên sẽ hủy bỏ áp dụng của lệnh migration gần nhất
Nó sẽ thực thì hàm down trong file migration
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
migrate:refresh
php artisan migrate:refresh
Lệnh trên sẽ chạy lần lượt 2 lệnh migrate và rollback, nghĩa là nó xóa migration rồi áp dụng lại
Lưu ý quan trọng: Trước khi thực hành, hãy tạo một databaase trong phpmyadmin VD `ql_ban_hang
Sau đó tìm và mở file
.env
tại thưc mục gốc của dự án và tiến hành cấu hình kết nới tới cơ sở dữ liệuql_ban_hang
vừa tạo ở trên
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= ql_ban_hang
DB_USERNAME=root
DB_PASSWORD=
Sau đó tiến hành các bước dưới đây
Mở cmd trong thư mục dự án và chạy lệnh Lện tạo model + migration Category
php artisan make::model Category -m
Lện tạo Controller resource
Lệnh này cần chỉ rõ tên model như dưới đây
php artisan make:controller ProductController --model=Product
2 Model trong thư mục
App\Models
2 migration trong thư mục
database\migrations
database\migrations\2025_08_13_081437_create_products_table.php
và khai báo như sau:<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id(); // id bigint unsigned primary key auto_increment
$table->string('name'); //name varchar(255) NOT NULL
$table->tinyInteger('status')->nullable()->default(1); /// status tinyint null default(1)
$table->timestamps(); // created_at, updated_at
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('categories');
}
};
database\migrations\2025_08_13_081437_create_products_table.php
và khai báo như sau:<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name', 100)->unique(); //name varchar(100) NOT NULL
$table->float('price', 10,2); //price float(10,2)
$table->string('image'); //image varchar(255) NOT NULL
$table->tinyInteger('status')->nullable()->default(1); /// status tinyint null default(1)
$table->unsignedBigInteger('category_id'); // Foreign key column
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
Mở cmd và chạy lệnh
php artisan migrate
Lệnh trên sẽ tìm và áp dụng tất cả các file migratiion trong thư mục
database\migrations
và tạo cấu trúc các bảng theo thông tin khai báo trong các file đó
Nó sẽ chạy hàm up trong file migration
Kiểm tra lại database để xem cấu trúc các bảng đã được tạo
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id(); // id bigint unsigned primary key auto_increment
$table->string('name'); //name varchar(255) NOT NULL
$table->tinyInteger('status')->nullable()->default(1); /// status tinyint null default(1)
$table->timestamps(); // created_at, updated_at
});
}
php artisan migrate:rollback
- Lệnh trên sẽ hủy bỏ áp dụng của lệnh migration gần nhất
- Nó sẽ thực thì hàm down trong file migration
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
php artisan migrate:refresh
Lệnh trên sẽ chạy lần lượt 2 lệnh migrate và rollback, nghĩa là nó xóa migration rồi áp dụng lại