Laravel:如何在刀片视图中拆分/获取json嵌套元素?

问题描述 投票:0回答:2

[当我通过下面的视图时,刀片显示了从mongo获取的整个Json结构。

{!! $fees !!}

输出:

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

现在如何获取嵌套的内容。我想在刀片中显示Fees = $ 200和Month = June)>

经过这样的尝试,但结果是刀片上的'空白'而没有任何错误。嵌套的open / closes:[是否存在任何问题,它在上述JSOn输入中的'University:'之后。请建议

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

我之前的步骤在这里给出:Laravel not fetching result from mongodb

编辑(1):

我尝试过这样,所以刀片视图上的结构。

<?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)
?>

输出:

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

也,

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

输出:

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

也是从Controller我也尝试如下:

..
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);

 }
  ..

编辑(2):

在刀片中,我尝试了如下操作,但由于出现异常:试图获取非对象的属性“费用”>

<?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

根据Chandan的建议编辑(3)。

<?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; 
} 
}
?>

输出:

 fees $200month June

仅输出是合并而没有逗号分隔的内容。我可以如下显示它们吗?>

费用= $ 200月=六月

或作为html

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

[当我通过下面的视图时,刀片显示了从mongo获取的整个Json结构。 {!! $ fees !!}输出:[{“ _id”:“ 5e04a06ca445f78401a16d0a”,“大学”:[{“ fees”:“ $ 200”},{“ month”:“ June” ...

< [
它的工作方式如下:-

@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

您可以尝试以下方法:

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

php json laravel mongodb
2个回答
0
投票
它的工作方式如下:-

0
投票
您可以尝试以下方法:

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

© www.soinside.com 2019 - 2024. All rights reserved.