diff --git a/app/Models/Category.php b/app/Models/Category.php index 9065e713..57dbb38c 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -19,9 +19,16 @@ class Category extends Model public $timestamps = false; protected $fillable = [ - 'name', 'icon', 'description', + 'name', 'icon', 'description', 'is_default', ]; + protected function casts(): array + { + return [ + 'is_default' => 'boolean', + ]; + } + public function posts(): HasMany { return $this->hasMany(Post::class); diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php index 28ec0d68..459eb6dc 100644 --- a/database/factories/CategoryFactory.php +++ b/database/factories/CategoryFactory.php @@ -15,6 +15,7 @@ public function definition(): array 'name' => $this->faker->name(), 'icon' => $this->faker->bothify('##### #####'), 'description' => $this->faker->sentence(), + 'is_default' => false, ]; } } diff --git a/database/migrations/2026_07_01_145044_add_is_default_to_categories_table.php b/database/migrations/2026_07_01_145044_add_is_default_to_categories_table.php new file mode 100644 index 00000000..2e0362e1 --- /dev/null +++ b/database/migrations/2026_07_01_145044_add_is_default_to_categories_table.php @@ -0,0 +1,33 @@ +boolean('is_default')->default(false)->after('description')->comment('是否為預設分類'); + }); + + DB::table('categories') + ->whereIn('name', ['日常分享', '程式技術', '電玩遊戲']) + ->update(['is_default' => true]); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('categories', function (Blueprint $table) { + $table->dropColumn('is_default'); + }); + } +}; diff --git a/tests/Feature/Models/CategoryTest.php b/tests/Feature/Models/CategoryTest.php index fbe0c4b6..b7dace33 100644 --- a/tests/Feature/Models/CategoryTest.php +++ b/tests/Feature/Models/CategoryTest.php @@ -22,3 +22,14 @@ 'name' => $category->name, ])); })->group('has link with name'); + +it('marks the seeded categories as default', function () { + expect(Category::whereIn('id', [1, 2, 3])->pluck('is_default')) + ->each->toBeTrue(); +}); + +it('defaults is_default to false for new categories', function () { + $category = Category::factory()->create(); + + expect($category->is_default)->toBeFalse(); +});