Laravel 使用模型实例急切加载模型关系?

jyc*_*753 2 php laravel

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

效果:

$payment->load(['invoice.source', 'invoice.user'])
            ->getRelations()['invoicePayments'];
Run Code Online (Sandbox Code Playgroud)

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

pat*_*cus 5

以下所有内容应该是等效的:

$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);
Run Code Online (Sandbox Code Playgroud)