簡體   English   中英

Angularjs + Laravel:獲取嵌套ng-repeat中雄辯關系的屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM