Schema Builder
Helper:
schema()(toàn cục)
Namespace:Illuminate\Database\Schema\Builder
Tài liệu tham khảo: Laravel Schema
1. Schema Builder là gì?
Schema Builder cung cấp các phương thức để tạo, sửa đổi và xóa cấu trúc bảng (table) trong database mà không cần viết câu lệnh CREATE TABLE hay ALTER TABLE thô. Đây chính là công cụ dùng khi bạn cần thiết lập cơ sở dữ liệu cho Plugin của mình.
SkillDo CMS v8 cung cấp hàm global schema() (trả về Illuminate\Database\Schema\Builder) để bạn gọi nhanh từ bất kỳ đâu.
2. File Migration trong Plugin
Các thao tác Schema trong Plugin thường được đặt trong một file database/database.php. File này được gọi tự động khi Plugin kích hoạt (activate) hoặc cập nhật (update).
Cấu trúc file chuẩn:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use SkillDo\Database\DB;
return new class () extends Migration {
public function up(): void
{
// Code tạo/sửa bảng khi Plugin kích hoạt
if (!schema()->hasTable('my_plugin_table')) {
schema()->create('my_plugin_table', function (Blueprint $table) {
// ... định nghĩa các cột
});
}
}
public function down(): void
{
// Code xóa bảng khi Plugin gỡ bỏ
schema()->drop('my_plugin_table');
}
};
3. Tạo Bảng (Create Table)
Dùng schema()->create($tableName, $callback) để tạo bảng. Luôn nên kiểm tra hasTable() trước để tránh lỗi nếu bảng đã tồn tại.
if (!schema()->hasTable('bookings')) {
schema()->create('bookings', function (Blueprint $table) {
$table->increments('id'); // ID tự tăng (PRIMARY KEY)
$table->string('name', 200); // Cột VARCHAR(200)
$table->string('phone', 20)->nullable(); // Nullable
$table->integer('service_id')->default(0);
$table->tinyInteger('status')->default(1);
$table->dateTime('booking_date')->nullable();
$table->text('note')->nullable();
$table->integer('user_created')->default(0);
$table->integer('user_updated')->default(0);
$table->dateTime('created')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->dateTime('updated')->nullable();
});
}
Cột Chuẩn SkillDo (Nên Dùng Cho Mọi Bảng Chính)
Hầu hết các bảng dữ liệu của CMS đều tuân theo quy ước:
| Cột | Kiểu | Mô tả |
|---|---|---|
id | increments | Khóa chính, tự tăng |
user_created | integer | ID Admin đã tạo bản ghi |
user_updated | integer | ID Admin đã cập nhật cuối |
created | dateTime | CURRENT_TIMESTAMP |
updated | dateTime | nullable |
order | integer | Vị trí hiển thị |
status / public | tinyInteger/string | Trạng thái hiển thị |
4. Các Kiểu Cột Phổ Biến (Column Types)
schema()->create('products', function (Blueprint $table) {
// Số nguyên
$table->increments('id'); // INT UNSIGNED AUTO_INCREMENT
$table->integer('price')->default(0); // INT
$table->tinyInteger('status')->default(1); // TINYINT
$table->unsignedBigInteger('user_id')->nullable(); // BIGINT UNSIGNED
// Chuỗi/Text
$table->string('title', 200); // VARCHAR(200)
$table->string('slug', 200)->collation('utf8mb4_unicode_ci');
$table->text('excerpt')->nullable(); // TEXT
$table->longText('content')->nullable(); // LONGTEXT
// Số thực
$table->float('rate')->default(1); // FLOAT
$table->decimal('amount', 10, 2); // DECIMAL(10,2)
// Thời gian
$table->dateTime('created')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->dateTime('updated')->nullable();
$table->timestamp('expires_at')->nullable();
// Logic
$table->boolean('is_active')->default(true); // TINYINT(1)
$table->enum('type', ['a', 'b', 'c'])->default('a'); // ENUM
// Char & UUID
$table->char('uuid', 36)->primary(); // CHAR(36) - dùng cho UUID
});
bigIncrements()
Phương thức bigIncrements tạo cột tương đương UNSIGNED BIGINT (khóa chính) tự động tăng:
$table->bigIncrements('id');
bigInteger()
Phương thức bigInteger tạo cột tương đương BIGINT
$table->bigInteger('votes');