简体   繁体   中英

Nested foreach in Laravel Blade

I am new to Laravel and I am trying to display data from the controller to view. I am able to display data from the first level of @foreach( $item->name, $item->address, etc) with ease. But I am having difficulty accessing a child of "subscriber_subscription" via Laravel blade convention. I tried accessing it using via code below:

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

JSON data used is:

[  
   {  
      "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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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