[英]Laravel eager load model relation using model instance?
有没有其他方法可以急切加载模态关系并避免使用['invoicePayments']
数组选择器?
外汇:
$payment->load(['invoice.source', 'invoice.user'])
->getRelations()['invoicePayments'];
主要原因是现在这样是因为我正在使用模型绑定注入,所以我的方法只是function getInvoicePayments(Payment $payment)
但我觉得这个数组选择是错误的,但我想不出任何其他解决方案? 有任何想法吗?
以下所有内容应该是等效的:
$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);
您也可以手动尝试访问每个关系的属性,这将强制 Laravel 在模型实例中加载关系:
$payment->invoice->source;
$payment->invoice->user;
$payment->invoicePayments;
我实际上不喜欢这样做,但这是另一种工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.