在Laravel中显示具有以下结构的JSON

时间:2015-06-11 14:45:59

标签: php json laravel

下午好。

我收到了这个JSON

{"id":130,"nif":"47812480B","name":"Bernat","cognoms":"Fernandez","file":"uploads\/fondo.jpg","birthday":"0000-00-00","presentacion":"presentacion","email":"bernat@gmail.com","password":"$2y$10$GfP1DYkTqyohjVzRcuuxaeKBSu7iJUBWJK6UDj7p681307uI6Ersq","idempresa":1,"id_poblacion":889,"id_online":0,"remember_token":null,"created_at":"2015-05-28 15:25:04","updated_at":"2015-05-27 15:25:04","municipio":{"id":889,"idprovincia":33,"poblacion":"Art\u00e9s","poblacionseo":"artes","postal":8271,"latitud":"41.798479","longitud":"1.954828"},"subastas":[{"id":16,"nombre":"HTC Wildfire","descripcion":"Descripcion","precio_salida":125,"cant_actual":125,"id_estado":0,"id_metode_envio":1,"id_metodo_pago":1,"id_creador":130,"id_ganador":125,"id_categoria":33,"id_adquirido":1,"data_inici":"2015-06-13","data_final":"2015-06-13","durada":1,"created_at":"2015-06-06 09:09:54","updated_at":"2015-06-06 09:55:49"}]}<p>34.884533408812</p> {"id":131,"nif":"263","name":"Lalo","cognoms":"Lelo","file":"uploads\/count.png","birthday":"0000-00-00","presentacion":"jejjsjsj","email":"vsdfsfaf@aasa.com","password":"$2y$10$QtrolUS9emCS7bfirsly9.JXt9AsYfAc.\/vA0iJCs47\/3g\/ypc8d6","idempresa":1,"id_poblacion":175,"id_online":0,"remember_token":"B1vEqdX8w42i7sLNvG201EArwSTrODe0DDzkVmjzj48ahMMV8oOLmzYRM5Mp","created_at":"2015-05-20 06:52:59","updated_at":"2015-06-06 08:35:13","municipio":{"id":175,"idprovincia":9,"poblacion":"Benej\u00fazar","poblacionseo":"benejuzar","postal":3390,"latitud":"38.083525","longitud":"-0.836494"},"subastas":[{"id":15,"nombre":"Nombre","descripcion":"Descripcion","precio_salida":142,"cant_actual":142,"id_estado":1,"id_metode_envio":1,"id_metodo_pago":1,"id_creador":131,"id_ganador":131,"id_categoria":33,"id_adquirido":1,"data_inici":"2015-06-18","data_final":"2015-06-17","durada":1,"created_at":"2015-06-06 00:00:00","updated_at":"2015-06-06 10:00:57"}]}<p>475.51158492016</p>

当我使用以下foreach时,我会显示信息

@foreach ($subastas as $subasta)    
{{$subasta}}
@endforeach

现在我尝试使用以下代码

@foreach ($subastas as $subasta)    
    @foreach ($subasta->subastas as $sub)   
        {{$sub}}
    @endforeach
@endforeach     

我收到了这个错误

Trying to get property of non-object (View: C:\xampp3\htdocs\laravel\resources\views\prodcercanos.blade.php)

针对此问题的任何解决方案?

问题是,我如何在laravel中呈现json?

接收JSON的控制器

public function productoscercanos ($lat,$long){
        $subastas = Subasta::all();
        $categorias = Categoria::all();
        $provincias = Provincia::all();
        $cercanos = User::with(array('municipio', 'subastas'))->get();
        $coordA   = Geotools::coordinate([$lat,$long]); 
        $output = [];
        foreach ($cercanos as $p) {
            if(count($p->subastas)>0){
            $coordB   = Geotools::coordinate([$p->municipio->latitud,$p->municipio->longitud]);
            $distance = Geotools::distance()->setFrom($coordA)->setTo($coordB);
            $dis = $distance->in('km')->haversine();
            $output[$dis] = $p."<p>$dis</p>";
            }
        }       
        ksort($output); 
        return view('prodcercanos')->with('categorias',$categorias)->with('provincias',$provincias)->with('subastas',$output);
    }   

3 个答案:

答案 0 :(得分:1)

$json = (your json file); $subasta = json_decode($json);  那么你现在应该能够将json用作对象了。

答案 1 :(得分:0)

您的代码正在输出确切定义的内容。首先看一下控制器如何构建阵列。

$output[$dis] = $p."<p>$dis</p>";

$ p是来自变量$ cercanos的User模型,将其附加到$ dis会导致它被转换为JSON并添加到您的视图迭代和输出的输出数组中。

看起来您正在尝试向用户添加距离并将其传递。

而是使用Accessor & Mutator向模型添加额外属性,然后将$ cercanos列表传递给视图,从那里您可以使用for each渲染并获取值。

@foreach($cercanos as $c)
   {{ $c->username }} {{ $c->dis }}
@endforeach

<强>更新

添加了访问者和mutator之后,您就可以使用内置方法的集合进行排序。

$cercanos->sortBy('dis');

答案 2 :(得分:0)

在第二个foreach循环中尝试数组语法。

@foreach ($subasta['subastas'] as $sub)   
    {{$sub}}
@endforeach