Laravel:如何从同一表返回父级>子级关系

时间:2018-07-30 05:14:34

标签: php laravel laravel-5.5

我有3张桌子product, category, attributes。在attributes表中,有parent_id用于同一表中的子属性。使用循环获取数据。

我的代码:

    $productDetails = Product::with('categories')->get();
    foreach ($productDetails as $key => $value) {
       foreach ($value->categories as $key => $value) {
            $attributes = Attribute::where('product_id',$value->product_id)->where('category_id',$value->id)->where('parent_id',Null)->get();
            $value->Attributes = $attributes;
            foreach ($attributes as $key => $value) {
                    $subAttributes = Attribute::where('parent_id', $value->id)->get();
                    $value->subAttributes  = $subAttributes;
            }
       }
   }

输出:

{

"productDetails": [
    {
        "id": 1,
        "title": "Small Size Diamond",
        "icon": null,
        "status": "Active",
        "categories": [
            {
                "id": 1,
                "product_id": 1,
                "title": "Sieve Size",
                "status": "Active",
                "sort_order": 1,
                "Attributes": [
                    {
                        "id": 1,
                        "product_id": 1,
                        "category_id": 1,
                        "parent_id": null,
                        "title": "- 2.0",
                        "status": "Active",
                        "sort_order": 1,
                        "subAttributes": [
                            [
                                {
                                    "id": 9,
                                    "product_id": 1,
                                    "category_id": 1,
                                    "parent_id": 1, // Attributes table ID
                                    "title": "+ 0000 - 000",
                                    "status": "Active",
                                    "sort_order": 1
                                },
                                {
                                    "id": 10,
                                    "product_id": 1,
                                    "category_id": 1,
                                    "parent_id": 1,  // Attributes table ID
                                    "title": "+ 000 - 00",
                                    "status": "Active",
                                    "sort_order": 2
                                }
                            ]
                        ]
                    }
                ]
            }
        ]
    }
]}

问题出在我完成的第一个产品响应中,但是在循环中的其他产品中我没有得到subAttributes数据。我该怎么办?

1 个答案:

答案 0 :(得分:-1)

简单,请不要在所有foreach循环中使用相同的变量名, 您的代码应遵循

$productDetails = Product::with('categories')->get();
foreach ($productDetails as $pro_key => $pro_value) {
   foreach ($pro_value->categories as $cat_key => $cat_value) {
        $attributes = Attribute::where('product_id',$cat_value->product_id)->where('category_id',$cat_value->id)->where('parent_id',Null)->get();
        $cat_value->Attributes = $attributes;
        foreach ($attributes as $att_key => $att_value) {
                $subAttributes = Attribute::where('parent_id', $att_value->id)->get();
                $att_value->subAttributes  = $subAttributes;
        }
   }
}