<?php
 namespace App\Http\Middleware; use App\Services\BasketService; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; use Symfony\Component\HttpFoundation\Response; class BasketMiddleware { protected BasketService $basketService; public function __construct(BasketService $basketService) { $this->basketService = $basketService; } public function handle(Request $request, Closure $next): Response { if (! Session::has('basket_session_id')) { Session::put('basket_session_id', (string) \Illuminate\Support\Str::uuid()); } $sessionId = Session::get('basket_session_id'); $ipAddress = $request->ip(); $clientId = auth()->check() ? auth()->id() : null; $basket = $this->basketService->getOrCreateBasket($sessionId, $ipAddress, $clientId); $request->attributes->set('basket', $basket); $request->attributes->set('basket_session_id', $sessionId); view()->share('basket', $basket); view()->share('basket_session_id', $sessionId); return $next($request); } } 