【问题标题】:Show nested collecctions in laravel views在 laravel 视图中显示嵌套集合
【发布时间】:2019-11-23 22:12:57
【问题描述】:

我是 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 trow 循环 3 个级别,以便获得: ......AttributeGroup(每一个)

.............属性映射(每一个)

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

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

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

我怎样才能让它工作? 谢谢

【问题讨论】:

  • 刀片视图页面上有什么?应该有类似@foreach($items as item) @endforeach

标签: laravel eloquent laravel-blade


【解决方案1】:

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

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

【讨论】:

  • 需要双括号。
  • 该解决方案只显示一个代码,但如果映射有多个代码就是问题
  • @Pablo 映射是什么意思?属性组映射?每个属性组映射项都有foreach
猜你喜欢
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多