简体   繁体   English

在Laravel Blade中嵌套foreach

[英]Nested foreach in Laravel Blade

I am new to Laravel and I am trying to display data from the controller to view. 我是Laravel的新手,正在尝试显示控制器中的数据以进行查看。 I am able to display data from the first level of @foreach( $item->name, $item->address, etc) with ease. 我能够轻松显示@foreach( $item->name, $item->address, etc)的第一层数据。 But I am having difficulty accessing a child of "subscriber_subscription" via Laravel blade convention. 但是我很难通过Laravel刀片约定访问“ subscriber_subscription”的子级。 I tried accessing it using via code below: 我尝试使用下面的代码访问它:

@foreach ($items as $item)
    ...
    @foreach($item->subscriber_subscription as $subs)
    ...
    ...
    @endforeach
    ...
    ...
@endforeach

JSON data used is: 使用的JSON数据是:

[  
   {  
      "id":1,
      "name":"Jollibee",
      "address":"31st cor. 9th Strata bldg. Ortigas Ave. Pasig City, 600",
      "contact":"87000",
      "email":"admin@jollibee.com.ph",
      "created_at":"2017-09-17 01:04:11",
      "updated_at":"2017-09-17 01:04:12",
      "subscriber_code":"jollibee",
      "subscriber_subscription":[  
         {  
            "id":1,
            "subscriber_id":1,
            "subscription_id":1,
            "start_date":"2017-09-17 01:35:27",
            "end_date":"2018-09-17 01:35:27",
            "created_at":"2017-09-17 01:35:23",
            "updated_at":"2017-09-17 01:35:23"
         },
         {  
            "id":6,
            "subscriber_id":1,
            "subscription_id":1,
            "start_date":"2017-10-17 01:35:27",
            "end_date":"2018-10-17 01:35:27",
            "created_at":"2017-09-17 01:35:23",
            "updated_at":"2017-09-17 01:35:23"
         }
      ]
   },
   {  
      "id":2,
      "name":"Bench",
      "address":"2207 Tektite Bldg. Ortigas Ave. Pasig City, 1600",
      "contact":"+639171234567",
      "email":"admin@benchtm.com.ph",
      "created_at":"2017-09-17 01:47:10",
      "updated_at":"2017-09-17 01:47:11",
      "subscriber_code":"bench",
      "subscriber_subscription":[  
         {  
            "id":4,
            "subscriber_id":2,
            "subscription_id":1,
            "start_date":"2017-09-17 14:19:15",
            "end_date":"2018-09-17 14:19:16",
            "created_at":"2017-09-17 14:19:20",
            "updated_at":"2017-09-17 14:19:20"
         },
         {  
            "id":5,
            "subscriber_id":2,
            "subscription_id":2,
            "start_date":"2017-09-17 14:19:15",
            "end_date":"2018-09-17 14:19:16",
            "created_at":"2017-09-17 14:19:20",
            "updated_at":"2017-09-17 14:19:20"
         }
      ]
   }
]

I'm getting this error: Invalid argument supplied for foreach() 我收到此错误: Invalid argument supplied for foreach()

Any help will be appreciated. 任何帮助将不胜感激。 Peace :-) 和平:-)

Thanks 谢谢

I've solved it. 我已经解决了 Apparently, i have to use $item->SubscriberSubscription which is the name of my model 显然,我必须使用$ item-> SubscriberSubscription这是我的模型的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM