Basket.php
public function products(){
return $this->belongsToMany(Product::class)->withPivot('quantity');
}
Products.php
public function baskets() {
return $this->belongsToMany(Basket::class)->withPivot('quantity');
}
вызываемый метод
public function add(Request $request, $id){
$basket_id = $request->cookie('basket_id');
$quantity = $request->input('quantity') ?? 1;
if (empty($basket_id)){
$basket = Basket::create();
$basket_id = $basket->id;
}else{
$basket = Basket::findOrFail($basket_id);
$basket->touch();
}
if ($basket->products->contains($id)) {
$pivotRow = $basket->products()->where('product_id',$id)->first()->pivot;
$quantity = $pivotRow->quantity + $quantity;
$pivotRow->update(['quantity' => $quantity]);
}else{
$basket->products()->atach($id,['quantity' => $quantity]);
}
return back()->withCookie(cookie('basket_id',$basket_id,525600));
}
$basket->products()->atach($id,['quantity' => $quantity]);
на этой строчке выпадает ошибка