我試圖做的是在嵌套的ng-repeat
中獲得雄辯關係的最深層次結構的屬性。Angularjs + Laravel:在嵌套的ng-repeat中獲取雄辯關係的屬性
這是關係。
Order ..hasMany.. SubItems ..belongTo.. Product
有一個模型Order
,其hasMany
模型SubItems
。 SubItems
型號belongsTo
型號Product
。
我有一個Order
的數組,我想從Order
的每個對象訪問相關的Product
和ng-repeat
。
<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;
}
請包含您的控制器代碼。 'product'一定不能加載到你的'subItem'模型上,所以我們需要看你的控制器以幫助 – Jeff
謝謝Jeff。我添加了它。相信與否,當我使用Laravel的forloop時,沒有錯誤,而'product'與'subitem'有關。 – Hiroki
當你調用'$ subItem-> product'時,它會自動爲你加載它。如果您希望它以JSON格式顯示,您必須手動加載它 – Jeff