user()->profitCenters()->first()->id; debug([$productId, $profitCenterId]); $ProfitCenter = ProfitCenter::find($profitCenterId); $favorites = $ProfitCenter->favorites()->where('product_id', '=', $productId)->get()->pluck('id')->toArray(); if (! in_array($productId, $favorites)) { $currentStatus = false; debug('berakjuk'); $ProfitCenter->favorites()->attach($productId); $currentStatus = true; } else { debug('kivesszuk'); $currentStatus = true; $ProfitCenter->favorites()->detach($productId); $currentStatus = false; } return \response()->json([ 'success' => true, 'status' => $currentStatus, ]); } }