schema([ Forms\Components\Select::make('product_id') ->label('Produkt') ->native(false) ->searchable() ->options(Product::all()->pluck('title', 'id')) ->reactive() ->afterStateUpdated(fn(callable $set) => $set('size_id', null)) ->required() ->disabled(fn($record) => !is_null($record)), Forms\Components\Select::make('size_id') ->label('Größe') ->native(false) ->options(function (callable $get) { $productId = $get('product_id'); if (!$productId) { return []; } return ProductSize::where('product_id', $productId) ->get() ->mapWithKeys(function ($product) { return [$product->id => "{$product->description} ({$product->price}€)"]; }); }) ->required() ->disabled(fn($record) => !is_null($record)) ->reactive(), Forms\Components\TextInput::make('amount') ->label('Mange') ->numeric(false) ->step(1) ->rules(['min:1']) ->default(1) ->required() ]); } public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('product.title')->label('Produkt'), Tables\Columns\TextColumn::make('size')->label('Größe') ->formatStateUsing(fn(ProductInventory $record) => "{$record->size->description} ({$record->size->price}€)"), Tables\Columns\TextColumn::make('total')->label('Anzahl'), ]) ->emptyStateHeading('Kein Inventar') ->recordUrl(null); } protected function getTableQuery(): Builder { return ProductInventory::select( DB::raw('MIN(id) as id'), DB::raw('product_id'), DB::raw('size_id'), DB::raw('count(*) as total') ) ->where() ->groupBy('size_id'); } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->whereNull('out_at') ->select(DB::raw('MIN(id) AS id'), DB::raw('MIN(product_id) AS product_id'), 'size_id', DB::raw('COUNT(*) as total')) ->groupBy('size_id') ->orderBy('product_id'); } public static function getPages(): array { return [ 'index' => Pages\ListProductInventory::route('/'), 'create' => Pages\CreateProductInventory::route('/create'), 'edit' => Pages\EditProductInventory::route('/{record}/edit'), ]; } public static function getPluralLabel(): string { return 'Inventar'; } }