[英]Angularjs + Laravel: Get properties of Eloquent Relationships in nested ng-repeat
我想做的是在嵌套的ng-repeat
獲得最深層次的口才關系的屬性。
這是關系。
Order ..hasMany.. SubItems ..belongTo.. Product
有一個模型Order
,其中hasMany
模型SubItems
。 此SubItems
模型belongsTo
模型Product
。
我有一個Order
數組,我想從每個Order
對象使用ng-repeat
訪問相關Product
。
<table>
<tbody ng-repeat="order in tc.orders" >//I use "as" syntax, so I use "tc."here
<tr ng-click="tc.showOrderDetail(order.id)">
<td ng-bind="order.id"></td>
<!-- "order.id" works in both lines -->
</tr>
<tr ng-repeat="subItem in order.subItems">
<td ng-bind="subItem.price"></td>
<!-- This "subItem.price" appears -->
<td ng-bind="subItem.product.name"></td>
<!-- This "subItem.product.name" doesn't appear -->
</tr>
</tbody>
</table>
關系的第二級( SubItems
)可以通過放置來訪問.
,但顯然我需要使用其他方式來訪問第三級( Product
)。
如果您能提供任何建議,我將不勝感激。
附加信息
當我使用Laravel的foreach循環時,可以通過...訪問
{{$subItem->product->name}} //This works fine!
控制器方法是...
public function getOrders()
{
$orders = $this->order->with('customer', 'subitems')->orderBy('id', 'desc')->get();
return $orders;
}
在您的代碼中,您並沒有告訴Laravel渴望將項目模型加載到您的Subitem模型上。 您需要像這樣添加渴望加載的產品:
$this->order->with('customer', 'subitems.product')->orderBy('id', 'desc')->get();
並像訪問其他任何javascript對象subItem.product.name
一樣訪問它
它在Laravel foreach循環中為您工作的原因是,當您請求諸如Laravel中的屬性(例如$subItem->product
而不是方法時,Laravel自動將關系Eloquent對象實例化為Model對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.