简体   繁体   中英

Laravel : How to split/fetch the json nested elements in blade view?

When i am passing the below in view blade shows the entire Json structure fetching from mongo.

{!! $fees !!}

output :

[{"_id":"5e04a06ca445f78401a16d0a","University":[{"fees":"$200"},{"month":"June"}]}]

Now how to fetch the nested content.I want to show Fees = $200 and Month = June in the blade

Tried like this but result is 'blank' on the blade without any error. Is there any issue with the nested open/closing :[, which is just after 'University:' in the above JSOn input. Please suggest

@foreach ($fees as $value)   
    {{$content[] = $value->['University'] }}  
    @foreach ($content as $key)
        <h1>{{ $key['fees'] }}</h1>
        <h1>{{ $key['month'] }}</h1>
    @endforeach
@endforeach

My earlier steps are given here : Laravel not fetching result from mongodb

Edit(1) :

I tried like this so the structure on blade view.

<?php
$get_result = json_decode($fees, true);

#$get_result = implode(",", array_flatten($get_result));
#print_r($get_result);
#$get_result2 = json_encode($get_result, true);

echo "</br>";
print_r($get_result)
?>

output :

Array ([0] => Array ([_id] => 5e04a06ca445f78401a16d0a [University] 
=> Array ([0] => Array ( [fees] => $200 ) [1] => Array ( [month] => June ) )))

Also,

<?php
echo htmlentities (print_r (json_encode ($fees), true));
?>

output:

"[{\"_id\":\"5e04a06ca445f78401a16d0a\",
   \"University\":[{\"fees\":\"$200\"},{\"month\":\"June\"}]}]"

Also from Controller i tried as below:

..
public function getFees()
{

  # database fetch test (with any one of the db query)
  $fees = Finance::all();

  $fees = Finance::where(['University.month' => 'June'])->get()->toArray();

  $fees = Finance::where('University.month', 'June')->first();

  $fees = Finance::where('University.month', 'June')->get();


  # Return test (with any one of the return)
  return view('tables')->with('fees', json_encode($fees, true));

  return view('tables', compact('fees'));

  return view('tables')->with(compact('fees'));

  return view('tables')->with('fees', $fees);

 }
  ..

Edit(2) :

in view blade i tried as below but getting exception as : Trying to get property 'fees' of non-object

<?php
$fees = json_decode($fees, true); 
#echo "</br>";
#print_r($fees)
?>

    @foreach ($fees[0] as $value)   
    @php $content = $value->University @endphp  // or without @
    @foreach ($content as $key)
        <h1>{{ $key['fees'] }}</h1>
        <h1>{{ $key['month'] }}</h1>
    @endforeach
@endforeach

Edit(3) as per suggestion by Chandan.

<?php

$fees = json_decode($fees); 
$univ = $fees[0]->University; 
//print_r($univ); 

foreach ($univ as $key => $value) 
{ 
foreach($univ[$key] AS $k =>$v)
{ 
echo $k." " .$v; 
} 
}
?>

output :

 fees $200month June

only thing the output is merges without comma separated. Can I show them as below

fees = $200 month = June

or as a html

<td>{{$k}}</td><td>{{$v}}</td>

You can try this:

$fees = json_decode($fees);
$univ = $fees[0]->University;
//print_r($univ);
foreach ($univ as $key => $value) {
  foreach($univ[$key] as $k =>$v){
     echo $k .'= '.$v.' ';
  }
}

It should be working as below:-

@foreach ($fees as $value)   
    {{$content[] = $value->['University'] }} 

    @if($content->['fees']!==null)
        <h1>{{ $content['fees'] }}</h1>
    @else
        <h1>-</h1>
    @endif

    @if($content->['fees']!==null)
        <h1>{{ $content['fees'] }}</h1>
    @else
        <h1>-</h1>
    @endif

@endforeach

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