Просмотр исходного кода

backend news category with default

CodeCommitAccess+1-at-632418466995 3 месяцев назад
Родитель
Сommit
b3bf32409e
1 измененных файлов: 11 добавлений и 1 удалений
  1. 11
    1
      app/Filament/Resources/NewsResource.php

+ 11
- 1
app/Filament/Resources/NewsResource.php Просмотреть файл

@@ -51,8 +51,18 @@ class NewsResource extends Resource
51 51
                         ->options(NewsCategory::get()->pluck("name","id"))
52 52
                         ->label("文章分類")
53 53
                         ->columnSpan(1)
54
+                        ->default(1)
54 55
                         ->native(false)
55
-                        ->Live(),
56
+                        ->afterStateHydrated(function ($component, $state) {
57
+                            // ✅ 如果沒有值,設定為第一項
58
+                            if (empty($state)) {
59
+                                $firstCategoryId = NewsCategory::first()?->id;
60
+                                if ($firstCategoryId) {
61
+                                    $component->state($firstCategoryId);
62
+                                }
63
+                            }
64
+                        })
65
+                        ->hidden(),
56 66
                     ])->columnSpanFull()->columns(2),
57 67
                     Group::make()->schema([
58 68
                         DatePicker::make('post_date')