在Laravel视图中打印嵌套的json数据

时间:2017-02-07 00:31:41

标签: php json laravel-5 laravel-5.3

下面是从控制器返回的Json视图

JSon Data

   [  
       {  
          "id":2,
          "firstname":"abc",
          "lastname":"def",
          "email":"abc@ab.ac.uk",
          "role":1,
          "university":1,
          "school_dept":5,
          "year":2,
          "photo":"URL",
          "bio":"ObxBJIDO6IfOU0DIw8a5",
          "search_status":"available",
          "created_at":null,
          "updated_at":null,
          "languages":[  
             {  
                "id":3,
                "language":"Spanish",
                "pivot":{  
                   "user_id":2,
                   "language_id":3,
                   "type":"native"
                }
             },
             {  
                "id":4,
                "language":"Greek",
                "pivot":{  
                   "user_id":2,
                   "language_id":4,
                   "type":"learn"
                }
             }
          ],
          "hobbies":[  
             {  
                "id":2,
                "hobby":"Basketball",
                "pivot":{  
                   "user_id":2,
                   "hobbie_id":2
                }
             },
             {  
                "id":3,
                "hobby":"Skiing",
                "pivot":{  
                   "user_id":2,
                   "hobbie_id":3
                }
             },
             {  
                "id":4,
                "hobby":"Running",
                "pivot":{  
                   "user_id":2,
                   "hobbie_id":4
                }
             }
          ],
          "universities":{  
             "id":1,
             "university":"some Uni"
          },
          "years":{  
             "id":2,
             "year":"2nd"
          },
          "departments":{  
             "id":5,
             "department":"Languages and Intercultural Studies"
          }
       }
    ]

我可以提取ID,名字,姓氏,电子邮件,... 第一级数据

@foreach($users as $user)
 <tr>
      <td>{{$user->firstname}} </td>
      <td>{{$user->lastname}} </td>
      <td>{{$user->email}} </td>
      <td>{{$user->photo}} </td>
      <td>{{$user->bio}} </td>
      <td></td>
    </tr>

@endforeach

此外,部门,大学和年份二级嵌套数据

<td>{{$user['universities']->university}} </td>
<td>{{$user['departments']->department}} </td>
<td>{{$user['languages']->language}} </td>
<td>{{$user['years']->year}} </td>

但是如何拉出第三级嵌套数据加上它的一种数组。

例如:爱好和语言。

我希望显示以下语言:学习:英语教学:法语这必须通过检查类型字段是否在语言数组中学习或教授来实现字段称为类型。它告诉用户是否想要学习特定语言或教授该语言。所有用户都学习和教授这两种类型。

此外,对于爱好列表,如: hobby1,hobby2,hobby3,...

我希望以最有效和最有效的方式展示

以下是我试图显示3级数据但是条件似乎不起作用的技术。我需要根据类型获得语言。

@foreach($user->languages as $langs)
   @if($langs['pivot']->type =='native')
   <td>{{$langs['pivot']->language_id}}</td>
   @else
   <td>{{$langs['pivot']->language_id}}</td>
   @endif
@endforeach

2 个答案:

答案 0 :(得分:1)

我在Laravel中有类似的问题来提取图像网址。但我并不需要for循环。所以我想我发布了这个例子。

天气JSON。

{
  "coord": {
    "lon": -97.29,
    "lat": 32.63
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "02d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 297.61,
    "pressure": 1007,
    "humidity": 36,
    "temp_min": 296.15,
    "temp_max": 299.15
  },
  "visibility": 16093,
  "wind": {
    "speed": 4.6,
    "deg": 200,
    "gust": 7.2
  },
  "clouds": {
    "all": 5
  },
  "dt": 1490813400,
  "sys": {
    "type": 1,
    "id": 2625,
    "message": 0.3809,
    "country": "US",
    "sunrise": 1490789980,
    "sunset": 1490834873
  },
  "id": 4689708,
  "name": "Everman",
  "cod": 200
}

在控制器中

    $restful_data=json_decode($res->getBody(), true);//to use in first and second level
    $restful_icon_array=$restful_data['weather']; //to use in third level
    $restful_icon = $restful_icon_array[0]; //second level is array

    return view('dashboard',['posts'=>$posts])
        ->with(['restful_data'=>$restful_data])//send array for second level
        ->with(['icon_weather'=>$restful_icon]);//send array for third level

在视图中

 <p>{{$restful_data['coord']['lon']}}</p> //access to second level
 <img src="https://openweathermap.org/img/w/{{ $icon_weather['icon'] }}.png"> //access to third level

答案 1 :(得分:0)

您可以将json更改为数组:

$user = json_decode($jsonData, true);

第一级:

$user["firstname"];
$user["lastname"];

第二级:

$user["universities"]["university"];
$user["departments"]["department"];

第三级:

foreach ($user["languages"] as $language) {
    if ($language["pivot"]["type"] == "native") {
        $language["language"];
    } else {
        $language["language"];
    }
}