mohafiz's avatar

Eloquent relationships between services in microservice architecture

Hello, I am moving an application from monolithic to microservices approach in Lumen, in the monolithic app I have orders table and products table with a one to many relationship between them. Now in a microservices approach it's reasonable to have different service for each. My issue is that I used to have a page responsible for displaying user orders with products using 'with' keyword for eager loading, and also allows the user to filter result by the name of the product. I am asking what is the best way to accomplish this in a microservices approach.

0 likes
5 replies
marosmjartan's avatar

I think this may be helpful:

$orders = Order::whereHas('products', function($query)
{
    $query->where('name', $product_name);  // Querring the products table

})->get();

return $orders;
mohafiz's avatar

Sorry for inconvenience. I am using different database for each microservice so I can't use eloquent relationships, order database holds the order information and products database holds the products information. I want to be able to retrieve an order and all it's products. @marosmjartan

marosmjartan's avatar

Is it even possible for laravel or lumen to handle more than 1 database? Wow, I don't even know that. I recommend merge these 2 databases and create eloquent relationships. But if this is not possible for you for some reason. You should try something like this:

$products = Products::where("order_id", $order->id)->get();

return [
	"order" => $order,
	"products" => $products
];
topvillas's avatar
Level 46

Don't overthink it, just call the products microserive from the orders microservice.

But don't forget to cache the products.

1 like

Please or to participate in this conversation.