Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions database/factories/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function definition(): array
'name' => $this->faker->name(),
'icon' => $this->faker->bothify('##### #####'),
'description' => $this->faker->sentence(),
'is_default' => false,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('categories', function (Blueprint $table) {
$table->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');
});
}
};
11 changes: 11 additions & 0 deletions tests/Feature/Models/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Loading