I've tried to calculate the total sum of an order with help of this: Laravel Eloquent: Best Way to Calculate Total Price
I want the calculation to be done in the model, so I can use it in multiple controllers.
My code looks like this:
class Order extends Model
{
public function customer()
{
return $this->belongsTo('App\Customer');
}
public function orderItems()
{
return $this->hasMany('App\OrderItem');
}
public function total()
{
return $this->orderItems->sum(function($orderItem)
{
return $orderItem->net_price;
});
}
}
So I think I have done everything exactly like in the example, but I'm getting following error:
ErrorException in Model.php line 2696: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
Any help will be highly appreciated.
Thanks.