You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DepartmentResource.php 4.4KB

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