在Laravel Blade中嵌套的foreach

时间:2017-09-26 10:46:02

标签: php laravel blade

我是Laravel的新手,我正在尝试从控制器显示数据以进行查看。我可以轻松地显示@foreach( $item->name, $item->address, etc)的第一级数据。但我无法访问" subscriber_subscription"通过Laravel刀片常规。我尝试使用下面的代码访问它:

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

使用的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"
         }
      ]
   }
]

我收到此错误:Invalid argument supplied for foreach()

任何帮助将不胜感激。和平: - )

由于

1 个答案:

答案 0 :(得分:0)

我已经解决了。显然,我必须使用$ item-> SubscriberSubscription这是我的模型的名称