schema([ TextInput::make('name') ->required() ->label('Name'), Textarea::make('description') ->label('Beschreibung'), Repeater::make('spices') ->schema([ Select::make('spice_id') ->label('Rohstoff') ->options(Spice::all()->pluck('name', 'id')) ->searchable() ->required(), TextInput::make('amount_in_percent') ->label('Anteil (in Prozent)') ->numeric() ->required() ->rules(['min:1', 'max:100']), ]) ->label('Inhalt') ->columns(2) ->addActionLabel('Inhaltsstoff hinzufügen') ->afterStateHydrated(function ($state, $set, $record) { if ($record) { // Load the spices relationship with pivot data $record->load('spices'); // Map the spices and their pivot data to the format the repeater expects $spicesData = $record->spices->map(function ($spice) { return [ 'spice_id' => $spice->id, 'amount_in_percent' => $spice->pivot->amount_in_percent, ]; })->toArray(); // Set the state for the repeater field to the mapped spices data $set('spices', $spicesData); } return $state; }) ->rules([new SumEquals100()]), ]); } /* */ public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('name') ->label('Name') ->sortable() ->searchable(), TextColumn::make('description') ->label('Beschreibung') ->limit(50), TextColumn::make('spices.name') ->label('Inhalt') ->formatStateUsing(function ($state, $record) { $spices = $record->spices->take(3)->map(function ($spice) { return $spice->name . ' (' . round($spice->pivot->amount_in_percent) . '%)'; }); if ($record->spices->count() > 3) { $spices->push('... weitere'); } return $spices->implode(', '); }) ->limit(50), ]); } public static function getRecord($id) { return Recipe::with('spices')->findOrFail($id); // Eager load spices relationship } public static function getPages(): array { return [ 'index' => Pages\ListRecipes::route('/'), 'create' => Pages\CreateRecipe::route('/create'), 'edit' => Pages\EditRecipe::route('/{record}/edit'), ]; } public static function getPluralLabel(): string { return 'Rezepte'; } }