adding multiple categories to product

when i add multiple categories tothe product it saves. But when i go to the edit page the categories are removed, so i have to add them again.

in the product model i have added this:

' public function category(): BelongsToMany
{
return $this->belongsToMany(Category::class);
} '

in the category model i have added this:

' public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class);
} '

in the ProductResource this code:

' Forms\Components\BelongsToManyMultiSelect::make(name: 'category_id')
->preload()
->multiple()
->relationship(name: 'category', titleAttribute: 'name') // Display the category names
->required()
->searchable(), '

how can i fix this?
image.png
Was this page helpful?