Is the vat the same on all products?
$vat = $this->products->first()->vat;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Afternoon All, i hope you're all well?
I'm trying to calculate the total invoice value including the vat amount and am getting a bit stuck. I think i've been staring at it too long to see it clearly anymore.
I have 3 functions in the job model.
getTotalSaleAttribute gathers the total sale price (net) for all products relating to that order.
getTotalVatAttribute which is fine. This just takes away the total price (inc vat) from the vat amount.
getTotalSaleIncVatAttribute gathers the total sale price (net) for all products relating to that order & heres the bit that i'm getting stuck on, pulls the vat rate calculation and times's the net sale total by the vat rate.
The product table has the vat_id from the VAT table as it belongs to VAT, and the VAT table has the calc column which would be 1.2 (20%).
// Products belongsTo VAT
// Vat hasMany Products
public function getTotalSaleIncVatAttribute()
{
$total = 0;
$vat = 0;
// $vat = $this->products->vat->calc;
// Currently this gives ^ an error of Property [vat] does not exist on this collection instance
// Total net price of products related to the order
$total += $this->products->sum(function($product) {
return $product->sale;
});
// Total price of products related to the order times by the vat rate
return $total * $vat;
}
Hopefully it makes sense lol. Please could someone advise on where i might be going wrong?
Thank you in advance.
So something like
$total = $this->products->sum(function($product) {
return $product->sale * $product->tax->calc;
});
Please or to participate in this conversation.