Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kamhawy's avatar

i can't insert data into Pivot table by laravel nova

salam-alaikum

I am working on the project by laravel nova and I can't insert data into Pivot table.

let me describe what my problem is.

I have 4 tables.

1- product

  • id
  • title
  • content

2- attributes

  • id
  • name

3- attribute_values

  • id
  • name
  • attribute_id

4 - product_attribute_values - ( Pivot table )

  • product_id
  • attribute_id
  • attribute_values_id

After that, I make Resource (AttributeValues) by Nova to insert the value of attribute into (attribute_values) and the same Resource I insert data into product_attribute_values - ( Pivot table ). Actually, I can insert product_id and attribute_values_id into product_attribute_values but I can't insert attribute_id

this is my Resource.

public function fields(NovaRequest $request) {

    return [

        ID::make()->sortable(),

        Text::make('name')->showWhenPeeking()
            ->sortable()
            ->rules('required', 'max:255'),

        Text::make('description')
            ->sortable()
            ->rules('required', 'max:5000'),

        BelongsTo::make('Attribute'),

        Multiselect::make('Product','ProductAttributeValue')
            ->options(\App\Models\Product::pluck('title', 'id'))
            ->belongsToMany(\App\Nova\Product::class),

    ];
}
0 likes
5 replies
MohamedTammam's avatar

Alaikum-salaam

Create a model for product_attribute_values, it would be called ProductAttributeValue.

Then create a resource for that model that accept the three value for product, attributes and attribute values IDs

kamhawy's avatar

@MohamedTammam I already did this!

I wrote this relation in the product model

public function ProductAttributeValue() {

    return $this->belongsToMany(AttributeValue::class,'product_attribute_values','product_id','attribute_values_id');

}
MohamedTammam's avatar

@kamhawy I don't see that you did that. Did you create a model for the pivot table product_attribute_values?

kamhawy's avatar

@MohamedTammam

yes

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;

class ProductAttributeValues extends Model { use HasFactory; }

Please or to participate in this conversation.