d2d.emegrendeles.hu/app/Http/Controllers/ProductController.php
E98Developer 68b7c35bef git init
2026-02-28 06:53:05 +01:00

37 lines
1.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\ProfitCenter;
class ProductController extends Controller
{
//
public function toggleFavorites($productId)
{
// $productId=878;
$profitCenterId = auth()->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,
]);
}
}