瀏覽代碼

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')