schema([ Forms\Components\TextInput::make('name') ->required() ->maxLength(255) ->live(onBlur: true) ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) { if (($get('slug') ?? '') !== Str::slug($old)) { return; } $set('slug', Str::slug($state)); }), Forms\Components\TextInput::make('slug') ->required(), Forms\Components\RichEditor::make('description') ->required(), Forms\Components\Select::make('parent_id') ->relationship('parent', 'name') ->searchable() ->preload() ->label('Parent Category') // Disallow having same category as it's own parent ->disableOptionWhen(fn (string $value, ?Category $record): bool => $record && (int) $value === $record->id), Forms\Components\FileUpload::make('image')->label('Image')->nullable()->acceptedFileTypes(['image/*'])->columnSpanFull(), ])->columns(2); } public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('name') ->searchable() ->sortable(), Tables\Columns\TextColumn::make('slug') ->searchable() ->sortable(), ]) ->filters([ // ]) ->actions([ Tables\Actions\EditAction::make(), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make()->before(function (DeleteBulkAction $action) { foreach ($action->getRecords() as $record) { if ($record->products()->exists() || $record->children()->exists()) { Notification::make() ->title('Cannot delete category') ->body('The category has products or children categories.') ->duration(5000) ->icon('ri-error-warning-line') ->danger() ->send(); $action->cancel(); } } }), ]), ]) ->defaultSort(function (Builder $query): Builder { return $query ->orderBy('sort', 'asc'); }) ->reorderable('sort'); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListCategories::route('/'), 'create' => Pages\CreateCategory::route('/create'), 'edit' => Pages\EditCategory::route('/{record}/edit'), ]; } }