35 lines
1016 B
PHP
35 lines
1016 B
PHP
<?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');
|
|
});
|
|
}
|
|
};
|