在laravel视图中显示嵌套的集合

时间:2019-11-23 22:12:57

标签: laravel eloquent laravel-blade

我是Laravel的新手,这是我遇到的问题 有3个表具有这种关系:

AttributeGroup->一对多-> AttributeGroupMapping

AttributeGroupMapping->多对一->属性

我准备了模型和控制器,并得到了这个集合:

lluminate\Database\Eloquent\Collection {#3093
     all: [
       App\AttributeGroup {#3085
         id: 1,
         name: "General",
         position: 1,
         is_user_defined: 0,
         attribute_family_id: 1,
         created_at: "2019-11-23 20:46:27",
         updated_at: "2019-11-23 20:46:27",
         attributeGroupMapping: Illuminate\Database\Eloquent\Collection {#3079
           all: [
             App\AttributeGroupMapping {#3106
               attribute_id: 1,
               attribute_group_id: 1,
               position: 1,
               created_at: "2019-11-23 20:46:27",
               updated_at: "2019-11-23 20:46:27",
               Attribute: App\Attribute {#3109
                 id: 1,
                 code: "phone1",
                 admin_name: "Phone 1",
                 type: "text",
                 validation: null,
                 position: 1,
                 is_required: 1,
                 is_unique: 0,
                 value_per_locale: 0,
                 value_per_channel: 0,
                 is_filterable: 0,
                 is_configurable: 0,
                 is_user_defined: 0,
                 is_visible_on_front: 0,
                 swatch_type: null,
                 use_in_flat: 1,
                 created_at: "2019-11-23 20:46:27",
                 updated_at: "2019-11-23 20:46:27",
               },
             },
             App\AttributeGroupMapping {#3096
               attribute_id: 2,
               attribute_group_id: 1,
               position: 9,
               created_at: "2019-11-23 20:46:27",
               updated_at: "2019-11-23 20:46:27",
               Attribute: App\Attribute {#3117
                 id: 2,
                 code: "notes",
                 admin_name: "notes",
                 type: "textarea",
                 validation: null,
                 position: 9,
                 is_required: 0,
                 is_unique: 0,
                 value_per_locale: 0,
                 value_per_channel: 0,
                 is_filterable: 0,
                 is_configurable: 0,
                 is_user_defined: 0,
                 is_visible_on_front: 0,
                 swatch_type: null,
                 use_in_flat: 1,
                 created_at: "2019-11-23 20:46:27",
                 updated_at: "2019-11-23 20:46:27",
               },
             },
           ],
         },
       },
       App\AttributeGroup {#3077
         id: 2,
         name: "Detalle",
         position: 2,
         is_user_defined: 0,
         attribute_family_id: 1,
         created_at: "2019-11-23 20:46:27",
         updated_at: "2019-11-23 20:46:27",
         attributeGroupMapping: Illuminate\Database\Eloquent\Collection {#3070
           all: [],
         },
       },
     ],
   }

现在,我如何才能与foreach一起骑行拖曳3个等级,这样我可以获得: ......属性组(每个)

........................ AttributeMapping(每个)

....................................属性(每个)

这是我发送给视图的内容:

$attributesGroups= AttributeGroup::with('attributeGroupMapping.Attribute')->get();
return view('admin.contact-conf', compact('attributesGroups'));

我如何使其起作用? 谢谢

1 个答案:

答案 0 :(得分:0)

无论如何,您都可以通过dd(attributesGroups)看到自己在视图中传递的内容,我认为以下代码将在您的视图中起作用:

@foreach ($attributesGroups as $attributesGroup)
        <p>{{$attributesGroup->name}}<p>
        @foreach ($attributesGroup->attributeGroupMapping as $attributeGroupMappingItem)
            <p>{{$attributeGroupMappingItem->Attribute->code}}</p>
    @endforeach
@endforeach