Creating and updating pivot table with attributes in the relationmanager
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 recipe_ingredient table.
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'),
]);
}
for some reason it is creating a record in the Ingredients table instead of the pivot recipe_ingredient table.
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'),
]);
}