소스 검색

backend news category with default

부모
커밋
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')