您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DepartmentResource.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Filament\Company\Resources\Core;
  3. use App\Filament\Company\Resources\Core\DepartmentResource\Pages;
  4. use App\Filament\Company\Resources\Core\DepartmentResource\RelationManagers\ChildrenRelationManager;
  5. use App\Models\Core\Department;
  6. use Filament\Facades\Filament;
  7. use Filament\Forms;
  8. use Filament\Forms\Form;
  9. use Filament\Resources\Resource;
  10. use Filament\Tables;
  11. use Filament\Tables\Table;
  12. use Illuminate\Database\Eloquent\Builder;
  13. class DepartmentResource extends Resource
  14. {
  15. protected static ?string $model = Department::class;
  16. protected static ?string $modelLabel = 'Department';
  17. protected static ?string $navigationIcon = 'heroicon-o-square-3-stack-3d';
  18. protected static ?string $navigationGroup = 'HR';
  19. protected static ?string $slug = 'hr/departments';
  20. public static function getModelLabel(): string
  21. {
  22. $modelLabel = static::$modelLabel;
  23. return translate($modelLabel);
  24. }
  25. public static function getNavigationParentItem(): ?string
  26. {
  27. if (Filament::hasTopNavigation()) {
  28. return 'HR';
  29. }
  30. return null;
  31. }
  32. public static function form(Form $form): Form
  33. {
  34. return $form
  35. ->schema([
  36. Forms\Components\Section::make('General')
  37. ->schema([
  38. Forms\Components\TextInput::make('name')
  39. ->autofocus()
  40. ->required()
  41. ->localizeLabel()
  42. ->maxLength(100),
  43. Forms\Components\Select::make('manager_id')
  44. ->relationship(
  45. name: 'manager',
  46. titleAttribute: 'name',
  47. modifyQueryUsing: static function (Builder $query) {
  48. $company = auth()->user()->currentCompany;
  49. $companyUsers = $company->allUsers()->pluck('id')->toArray();
  50. return $query->whereIn('id', $companyUsers);
  51. }
  52. )
  53. ->localizeLabel()
  54. ->searchable()
  55. ->preload()
  56. ->nullable(),
  57. Forms\Components\Group::make()
  58. ->schema([
  59. Forms\Components\Select::make('parent_id')
  60. ->localizeLabel('Parent Department')
  61. ->relationship('parent', 'name')
  62. ->preload()
  63. ->searchable()
  64. ->nullable(),
  65. Forms\Components\Textarea::make('description')
  66. ->autosize()
  67. ->nullable()
  68. ->localizeLabel(),
  69. ])->columns(1),
  70. ])->columns(),
  71. ]);
  72. }
  73. public static function table(Table $table): Table
  74. {
  75. return $table
  76. ->columns([
  77. Tables\Columns\TextColumn::make('name')
  78. ->localizeLabel()
  79. ->weight('semibold')
  80. ->searchable()
  81. ->sortable(),
  82. Tables\Columns\TextColumn::make('manager.name')
  83. ->localizeLabel()
  84. ->searchable()
  85. ->sortable(),
  86. Tables\Columns\TextColumn::make('children_count')
  87. ->localizeLabel('Children')
  88. ->badge()
  89. ->counts('children')
  90. ->searchable()
  91. ->sortable(),
  92. ])
  93. ->filters([
  94. //
  95. ])
  96. ->actions([
  97. Tables\Actions\EditAction::make(),
  98. ])
  99. ->bulkActions([
  100. Tables\Actions\BulkActionGroup::make([
  101. Tables\Actions\DeleteBulkAction::make(),
  102. ]),
  103. ]);
  104. }
  105. public static function getRelations(): array
  106. {
  107. return [
  108. ChildrenRelationManager::class,
  109. ];
  110. }
  111. public static function getPages(): array
  112. {
  113. return [
  114. 'index' => Pages\ListDepartments::route('/'),
  115. 'create' => Pages\CreateDepartment::route('/create'),
  116. 'edit' => Pages\EditDepartment::route('/{record}/edit'),
  117. ];
  118. }
  119. }