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
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function orders(){
return $this->hasMany(Order::class);
}
Model: Orders.php
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function product(){
return $this->belongsTo(Product::class);
}
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:
products:
id\1
name\productname
price \1.00USD
id\2
name\productname2
price \1.00USD
It must return sum of all product so it must return 2.00USD
@Jonas Staudenmeir
many to many
relation with intermediate table. Doc – iamab.in$var
? – Jonas Staudenmeirsum
? You may try$order->product->price;
. If you havequantity
field in the order you may multiply the price with that. – iamab.in