使用模型实例的Laravel渴望加载模型关系?

时间:2017-04-20 22:00:06

标签: php laravel

还有其他方法可以加载模态关系并避免使用['invoicePayments']数组选择器吗?

fx:

$payment->load(['invoice.source', 'invoice.user'])
            ->getRelations()['invoicePayments'];

现在的主要原因是因为我正在使用模型绑定注入,所以我的方法只是function getInvoicePayments(Payment $payment)但我觉得这个数组选择是错误的,但我想不出任何其他解决方案它?任何想法?

1 个答案:

答案 0 :(得分:3)

以下所有内容均应相同:

$one = $payment->load(['invoice.source', 'invoice.user'])->getRelations()['invoicePayments'];

$two = $payment->load(['invoice.source', 'invoice.user'])->getRelation('invoicePayments');

$thr = $payment->load(['invoice.source', 'invoice.user'])->invoicePayments;

dd($one, $two, $thr);