Skip to content
Advertisement

Sum price of one to many relation ship (laravel)

i have problem with one to many relation ship! So im try to sum product prices so here is what i mean:

Model: Product.php

JavaScript

Model: Orders.php

JavaScript

So i want make smth like this: $var->product->price->sum();

My database if it needed:

Orders: id product_id|user_id| |-|----------|-------| |1|1 |1 | | | | |

And Products have only 4 columns: id,name,price,description

I mean that:

JavaScript

It must return sum of all product so it must return 2.00USD

@Jonas Staudenmeir

Advertisement

Answer

Using eloquent models without DB

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement