resource/database/migrations/2026_01_20_110012_add_field_articletable.php
2026-01-21 19:53:48 +08:00

35 lines
1016 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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::table('articles', function (Blueprint $table) {
// 阿诺提醒morphs('oSubject') 会生成 oSubject_id 和 oSubject_type
// 并且会自动建立复合索引,非常扎实!
$table->morphs('oSubject');
// 如果你之前已经有数据了,建议给这两个字段允许 null防止迁移报错
// $table->morphs('oSubject')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('articles', function (Blueprint $table) {
// 降级操作:把这两个字段和索引一并撤销,保持数据库清爽
$table->dropMorphs('oSubject');
});
}
};