Reputation: 231
I created a IngredientsRelationManager inside the RecipeResource: i see the table fine and get the add new ingredient button and modal: for some reason it is creating a record in the Ingredients table instead of the pivot and then adding it to the recipe_ingredient table as a new record with a new ingredient.
pivot relationships are Recipe Model: public function ingredients() { return $this->belongsToMany(Ingredient::class, 'recipe_ingredient', 'recipe_id', 'ingredient_id') ->withPivot('quantity', 'unit'); }
Ingredients Model:
public function recipes() { return $this->belongsToMany(Recipe::class, 'recipe_ingredient', 'recipe_id', 'ingredient_id') ->withPivot('quantity', 'unit'); }
code:
{
protected static string $relationship = 'ingredients';
public function form(Form $form): Form
{
$ingredients = Ingredient::all()->pluck('name', 'id')->toArray();
return $form
->schema([
Forms\Components\Select::make('ingredient_id')
->placeholder('Select Ingredients')
->searchable()
->columnSpan(1)
->required()
->options($ingredients),
Forms\Components\TextInput::make('quantity')->default(1),
Forms\Components\TextInput::make('unit')->default('1'),
]);
}
Upvotes: 0
Views: 1084
Reputation: 900
You can load the form fields values passing the values with the method mountUsing
:
Tables\Actions\EditAction::make()
->modalWidth('l')
->form(function ($action) {
// fields of the form
})
->mountUsing(
fn($record, $form) => $form->fill($record->pivot->toArray())
)
Upvotes: 1