Laravel Microservices- Breaking A — Monolith To M...

public function handle(OrderPlaced $event) foreach ($event->orderData['items'] as $item) Product::where('id', $item['product_id']) ->decrement('stock', $item['quantity']);

return response($response->body(), $response->status()); In a monolith, you had foreign keys like user_id in the orders table. Now, user_id exists only in Auth DB. In Order DB, you store auth_user_id as a string (UUID) , not a foreign key.

try $user = JWTAuth::parseToken()->authenticate(); catch (Exception $e) return response()->json(['error' => 'Unauthorized'], 401); // Inject the user ID from token into the request $request->merge(['authenticated_user_id' => $user->id]); Laravel Microservices- Breaking a Monolith to M...

return $next($request); When creating an order, the Order Service must check if the product exists and has stock in the Catalog Service.

Issue a JWT token from the Auth Service. All other services will verify the token's signature without hitting the Auth database. order-service: build:

order-service: build: ./order-service environment: SERVICES_CATALOG_URL: http://catalog-service:8000 RABBITMQ_HOST: rabbitmq ports: - "8003:8000"

Route::post('/auth/login', fn() => proxyTo('http://auth-service/api/login')); Route::get('/products', fn() => proxyTo('http://catalog-service/api/products')); Route::post('/orders', fn() => proxyTo('http://order-service/api/orders')); function proxyTo($url) $response = Http::withHeaders(request()->headers->all()) ->send(request()->method(), $url, [ 'query' => request()->query(), 'json' => request()->json()->all() ]); [ 'query' =&gt

In order-service :