拉雅维·埃尔昆特得到父母关系



我有一个模型"发票";。该模型与模型"hasMany"有一定的关系;发票行";。

public function invoiceLines()
{
return $this->hasMany('AppModelsInvoiceLine');
}

我想用正确的货币(如23.59美元和52.33欧元(显示几张发票的所有价格(简化(

$invoices = Invoice::where(........)->with('invoiceLines')->get();
foreach ($invoices as $invoice) {
foreach ($invoice->invoiceLines as $line) {
echo $line->formattedPrice();
}
}

因此,模型";InvoiceLine;具有一种方法";formattedPrice";

public function formattedPrice()
{
$invoice = ?????;
$currency = $invoice->currency;
$price = $this->price;
return formatPrice($price, $currency);
}

但我不知道如何从invoiceLine的父级"中获取货币属性;发票";。

InvoiceLine.php模型中添加反向关系。

public function invoice()
{
return $this->belongsTo(Invoice::class);
}

现在你可以这样访问你的发票。

$invoice = $this->invoice;
$currency = $invoice->currency;

您可以按以下划分合计

$euro_total = 0;
$usd_total = 0;
foreach ($invoices as $invoice) {
foreach ($invoice->invoiceLines as $line) {
if($invoice->currency == 'EURO'){
$euro_total = $euro_total + $line->price;
}
if($invoice->currency == 'USD'){
$usd_total = $euro_total + $line->price;
}
}
}
echo $euro_total;
echo $usd_total;

相关内容

  • 没有找到相关文章

最新更新