1

below is the Json returned from controller to view

JSon Data

   [  
       {  
          "id":2,
          "firstname":"abc",
          "lastname":"def",
          "email":"[email protected]",
          "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"
          }
       }
    ]

I am able to pull id, firstname,lastname, email, ... first level data

@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

Also, departments, university and years second level nested data

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

But how to pull third level nested data plus its a type of array.

for example: Hobbies and Languages both.

I want to show languages like: learn : English and teach: French this has to be achieved by checking if type field is learn or teach in language array with a field called type. which tells if a user wants to learn a specific language or teach that language. All users have learn and teach both types.

Also, for list of hobbies like: hobby1, hobby2, hobby3,...

I want to show this most neatest and efficient way possible

Below is the technique I am trying to show level 3 data but if condition does not seem to work. I need to get the language as per the type.

@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 Answers 2

1

You can change json to array:

$user = json_decode($jsonData, true);

First level :

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

Second level :

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

Third level :

foreach ($user["languages"] as $language) {
    if ($language["pivot"]["type"] == "native") {
        $language["language"];
    } else {
        $language["language"];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

can you show how to get the language depending on the type. I want to print if type = learn print language which is to learn else language native. How to do because the condition(type) is inside the pivot but the value is outside the pivot but inside the language.
1

I was having similar problem in Laravel to extract an image url. But I didn't need the for loop. So I thought I post the example.

Weather 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
}

In the controller

    $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

In the view

 <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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.