Laravel:如何在刀片视图中拆分/获取json嵌套元素? [英] Laravel : How to split/fetch the json nested elements in blade view?

查看:98
本文介绍了Laravel:如何在刀片视图中拆分/获取json嵌套元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{!! $fees !!}

输出:

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

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

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

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

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

我在此之前的步骤如下: Laravel无法从mongodb获取结果

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

编辑(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)
?>

输出:

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我尝试如下:

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

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

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

输出:

 fees $200month June

仅输出是合并而没有逗号分隔的情况. 我可以如下显示它们

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

费用= $ 200 月=六月

fees = $200 month = June

或html

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

推荐答案

您可以尝试以下操作:

$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.' ';
  }
}

这篇关于Laravel:如何在刀片视图中拆分/获取json嵌套元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆