Laravel mutator指定返回的字段

时间:2019-08-17 11:49:19

标签: laravel eloquent

我在RefundExportInvoices之间有关系。 此外,ExportInvoicesCustomerBranch也有关系。

Refund.php模型代码

public function exportInvoice () {
    return $this -> belongsTo('App\Models\Invoices\ExportInvoice', 'export_invoice_id');
}

ExportInvoice.php模型代码

public function customerBranch () {
    return $this -> belongsTo('App\Models\Customer\CustomerBranch');
}

我正在尝试做的事情:

我试图访问Refund -> ExportInvoice -> CustomerBranch并从客户分支address中获取一个字段并将其存储为Mutator

我做了什么:

在Refund.php中创建一个变量。

// APPENDS
protected $appends = [
    'customer_and_branch',
    'report_refund_total',
];

public function getCustomerAndBranchAttribute() {
    return $this -> exportInvoice -> customerBranch -> customer_and_branch;
}

这可以正常工作并返回我想要的,但是问题是:

它返回所有ExportInvoice信息,加上它返回所有customerBranch信息,我认为这是一个很大的负担。

我只想返回一个customer_and_branchExportInvoice信息的字段。

0 个答案:

没有答案
相关问题