How to get a sum() from joinTable.field * belongsToMany.field


Hi Folks !

I have the cliché situation: Orders belongsToMany Products through OrderItems.
And also Orders belongsTo Customers

I am trying to get a resultSet with, and a calculate field which
means (order.product.price * order.order_item.quantity)

I’have tried with ->matching(‘Products’) with however I’ve got duplicated order entities :frowning:

Is there any way to do that?

Thanks in advance !


There are belongsToMany association in between orders and products so it obvious that you will get duplicate orders. For avoiding this just try to add something like ->group(['']);, this will give you single entity by products.