many relation ?
here are the models :
class Category extends Model
{
public function products()
{
return $this->belongsToMany(
Product::class,
'product_category',
'category_id',
'product_id'
)->withTimestamps();
}
}
class Product extends Model
{
public function categories()
{
return $this->belongsToMany(
Category::class,
'product_category',
'product_id',
'category_id'
)->withTimestamps();
}
}
and here are my controller :
public function add_category(Product $product, EditProductCategory $request)
{
$product->categories()->attach($request->validated()['items']);
return response()->noContent(200);
}
public function remove_category(Product $product, EditProductCategory $request)
{
$product->categories()->detach($request->validated()['items']);
return response()->noContent(200);
}
Aren't you supposed to pass an instance of the Model into attach/detach
Обсуждают сегодня