adding multiple categories in ProductResource error

When i add multiple categories in the ProductResource, it saves them. But on the edit page the categories are removed.

this is the code in the product model:

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?

ps how can i add snippets of codes here?
image.png
Was this page helpful?