Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DepartmentResource.php 4.2KB

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