HistoryResource.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\HistoryResource\Pages;
  4. use App\Filament\Resources\HistoryResource\RelationManagers;
  5. use App\Models\History;
  6. use Filament\Forms;
  7. use Filament\Forms\Components\FileUpload;
  8. use Filament\Forms\Components\Group;
  9. use Filament\Forms\Components\Radio;
  10. use Filament\Forms\Components\Section;
  11. use Filament\Forms\Components\Select;
  12. use Filament\Forms\Components\TextInput;
  13. use Filament\Forms\Form;
  14. use Filament\Resources\Resource;
  15. use Filament\Tables;
  16. use Filament\Tables\Columns\ImageColumn;
  17. use Filament\Tables\Columns\TextColumn;
  18. use Filament\Tables\Filters\SelectFilter;
  19. use Filament\Tables\Table;
  20. use Illuminate\Database\Eloquent\Builder;
  21. use Illuminate\Database\Eloquent\SoftDeletingScope;
  22. class HistoryResource extends Resource
  23. {
  24. protected static ?string $model = History::class;
  25. protected static ?string $navigationIcon = 'heroicon-o-list-bullet';
  26. protected static ?string $navigationLabel = "宏匯里程管理";
  27. protected static ?string $navigationGroup = '關於宏匯';
  28. protected static ?string $modelLabel = "宏匯里程管理";
  29. public static function form(Form $form): Form
  30. {
  31. return $form
  32. ->schema([
  33. Section::make("")->schema([
  34. Select::make("selected_year")->label("年份")->options(function () {
  35. $currentYear = now()->year;
  36. $years = [];
  37. // 從 10 年前到 5 年後
  38. for ($i = $currentYear - 10; $i <= $currentYear; $i++) {
  39. $years[$i] = strval($i) . '年';
  40. }
  41. return $years;
  42. })->columnSpan(1),
  43. Select::make("selected_month")->label("月份")->options(function (){
  44. $months = [];
  45. for($i=1;$i<=12 ;$i++){
  46. $months[$i] = strval($i) . "月";
  47. }
  48. return $months;
  49. })->default(now()->month)->columnSpan(1),
  50. TextInput::make("title")->label("標題")->translatableTabs()->columnSpanFull(),
  51. Group::make()->schema([
  52. FileUpload::make("img_url")->label("圖片")->directory("histories")
  53. ->columnSpanFull(),
  54. TextInput::make("img_alt")->label("圖片註釋")->translatableTabs()->columnSpanFull()
  55. ])->columnSpanFull(),
  56. Radio::make("visible")->label("顯示/不顯示")->options([1 => "顯示", 0 => "不顯示"])->inline()->default(1)->columnSpan(2)
  57. ])->columns(3)
  58. ]);
  59. }
  60. public static function table(Table $table): Table
  61. {
  62. return $table
  63. ->columns([
  64. TextColumn::make("selected_year")->label("年份")->alignCenter(),
  65. TextColumn::make("selected_month")->label("月份")->alignCenter(),
  66. TextColumn::make("title")->label("標題"),
  67. ImageColumn::make("img_url")->alignCenter(),
  68. TextColumn::make('created_at')->label("建立時間")->date(),
  69. TextColumn::make('updated_at')->label("更新時間")->date(),
  70. ])
  71. ->filters([
  72. SelectFilter::make('selected_year')->label("年份")
  73. ->options(function () {
  74. $currentYear = now()->year;
  75. $years = [];
  76. // 從 10 年前到 5 年後
  77. for ($i = $currentYear - 10; $i <= $currentYear + 10; $i++) {
  78. $years[$i] = strval($i) . '年';
  79. }
  80. return $years;
  81. })
  82. ->attribute('selected_year'),
  83. SelectFilter::make('selected_month')->label("月份")
  84. ->options(function () {
  85. $months = [];
  86. for($i=1;$i<=12 ;$i++){
  87. $months[$i] = strval($i) . "月";
  88. }
  89. return $months;
  90. })
  91. ->attribute('selected_month'),
  92. ])
  93. ->actions([
  94. Tables\Actions\EditAction::make(),
  95. ])
  96. ->bulkActions([
  97. Tables\Actions\BulkActionGroup::make([
  98. Tables\Actions\DeleteBulkAction::make(),
  99. ]),
  100. ]);
  101. }
  102. public static function getRelations(): array
  103. {
  104. return [
  105. //
  106. ];
  107. }
  108. public static function getPages(): array
  109. {
  110. return [
  111. 'index' => Pages\ListHistories::route('/'),
  112. 'create' => Pages\CreateHistory::route('/create'),
  113. 'edit' => Pages\EditHistory::route('/{record}/edit'),
  114. ];
  115. }
  116. }