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