2016-07-30 109 views
1

我已经保存在我的数据库中的字段中的一些JSON数据,显示在一个视图中的数据,我在控制器JSON数据和Laravel视图

$gallerie = Articolo::select('nome_gal') 
        ->where('nome_gal','LIKE','%nome_gal%') 
        ->orderBy('created_at', 'desc') 
        ->orderBy('updated_at', 'desc') 
        ->take(6) 
        ->get(); 

写这个查询在控制器中,我通过该查询,和其他查询,以一个视图在视图的侧栏部分这样

return view('articoli')->with(array('articoli' => $articoli, 'gallerie'=>$gallerie, 'categorie'=> $categorie, 'trevideo'=>$trevideo, 'treaudio'=>$treaudio)); 

我用这个代码:

<div class="sidebar-item popular"> 
    <h3>Ultime foto</h3> 
    <ul class="gallery"> 
     @foreach(json_decode($gallerie, true) as $galleria) 
      <li>{{ $galleria['cover_gal'] }}</li> 
     @endforeach 
    </ul> 
</div> 

那么,因此,当我尝试加载页面时,我看不到<ul class="gallery">下的任何代码。

JSON数据的结构是这样的http://p4c.it/alfa.json

考虑到json_decode需要一个字符串,在我看来,有什么不对的控制器部分。你有什么建议吗?

+1

你尝试过这样的

  • {{$ galleria-> cover_gal}}
  • jonju

    +0

    cover_gal是JSON的一部分数据。 JSON数据的结构是这样的http://p4c.it/alfa.json – user1441917

    +0

    首先检查查询输出,我认为你的查询是错误的,在'like'子句中你指定了列名(%nome_gal%) , – Aparna

    回答

    0

    嗯,这是我做了什么和它的工作对我来说,在下面,一切都是概括,因此该解决方案是完整的为新读者。在该表的模型我定义的一个假想的属性,像这样:

    protected $appends=['att1','att2','att3', 'att4', 'nome_galla'];

    控制器

    ,显然

    public function getNomeGallaAttribute() { $json = $this -> attributes['nome_gal']; return json_decode($json, true); }

    我已经查询:

    $gallerie = Articolo::select('nome_gal')->where('nome_gal','LIKE','%nome_gal%')->orderBy('created_at', 'desc')->orderBy('updated_at', 'desc')->take(6)->get();

    显然是通道:

    return view('articoli')->with(array('articoli' => $articoli, 'gallerie'=>$gallerie, 'categorie'=> $categorie, 'trevideo'=>$trevideo, 'treaudio'=>$treaudio));

    终于,在视图中,我以这种方式解决:

    <div class="sidebar-item popular"> <h3>Ultime foto</h3> <ul class="gallery"> @foreach($gallerie as $galleria) <li><a href="#"><img src="gallerie/{{$galleria['nome_galla']['nome_della_gal'] }}/img_rid/{{$galleria['nome_galla']['cover_gal'] }}" alt=""></a></li> @endforeach </ul> </div>

    出于某种原因,常规语法

    {{asset("gallerie/$galleria['nome_galla']['nome_della_gal']/img_rid/$galleria['nome_galla']['cover_gal']")}}

    没有工作过我(因此导致死亡的白页)。

    感谢大家的贡献,我知道PHP,但我是新来的laravel :-)

    1

    看起来你的JSON数据有一定的问题: 如果考虑 'id_foto' & 'nome_foto' 一对那么它应该是

    "galleria":[ 
          {"id_foto": "1","nome_foto": "DSC_0006.JPG"}, 
          {"id_foto": "2","nome_foto": "DSC_0017.JPG"} 
          ] 
    

    ,而不是

    "galleria":[{ 
          "id_foto": "1", 
          "nome_foto": "DSC_0006.JPG", 
          "id_foto": "2", 
          "nome_foto": "DSC_0017.JPG", 
          "id_foto": "3" 
          }] 
    

    ,因为当我们json_decode()你的json数据我们只能得到最后的'id_foto'&'nome_foto'like

    [galleria] => Array ([0] => Array ([id_foto] => 24 [nome_foto] => foto_9.jpg)) 
    

    我的猜测是错误的,并检查了编辑视图代码

    控制器

    return view('articoli')->with(array('articoli' => $articoli, 
    'gallerie'=>json_decode($gallerie,true), 'categorie'=> $categorie, 
    'trevideo'=>$trevideo, 'treaudio'=>$treaudio)); 
    

    查看将帖子

    <div class="sidebar-item popular"> 
        <h3>Ultime foto</h3> 
        <ul class="gallery"> 
         @foreach($gallerie['galleria'] as $galleria=>$gal) 
          <li>{{{$gal['id_foto']}}} <br> {{{$gal['nome_foto']}}}</li> 
         @endforeach 
        </ul> 
    </div> 
    

    并访问键nome_gal & cover_gal

    $gallerie['nome_gal'] 
    $gallerie['cover_gal'] 
    
    +0

    整个页面是白色的:(我怀疑JSON数据无效,但是,当我尝试在测试路径中加载它时,一切正常[$ gallerie = \ App \ Articolo :: select('nome_gal' ,'id_articolo') - > where('nome_gal','LIKE','%nome_gal%') - > orderBy('created_at','desc') - > orderBy('updated_at','desc') - > take (34) - >得到(); 的foreach($ gallerie为$拱廊){$ 五倍子= json_decode($拱廊[ 'nome_gal'],TRUE); 回声

    '$ galleria-> id_articolo” - '。$ galla ['cover_gal']。' - '。$ galla ['nome_gal']。'

    '; }' – user1441917

    +0

    你能告诉我您的实际JSON数据? – jonju

    +0

    是的,这里是http://p4c.it/alfa.json – user1441917

    0

    您的json在环球免税店数组中有相同的密钥名称,所以它只取最后一个值。相反,你可以为

    { 
        "nome_gal": "just a name", 
        "cover_gal": "DSC_0017.JPG", 
        "galleria": [{ 
         "id_foto1": 1, 
         "nome_foto1": "DSC_0006.JPG", 
         "id_foto2": 2, 
         "nome_foto2": "DSC_0017.JPG", 
         "id_foto3": 3, 
         "nome_foto3": "DSC_0018.JPG" 
        }] 
    } 
    

    这里用的是你的JSON解码阵列

    Array 
    (
        [nome_gal] => just a name 
        [cover_gal] => DSC_0017.JPG 
        [galleria] => Array 
         (
          [0] => Array 
           (
            [id_foto] => 24 
            [nome_foto] => foto_9.jpg 
           ) 
    
         ) 
    
    ) 
    
    +0

    事实上,这是我纠正http://p4c.it/gamma.json的问题之一。无论如何,我试图访问cover_gal而不是拱廊。我也尝试简化我的JSON,消除了数组部分,但没有成功。在这一刻,我正在寻找一个传递给视图的json的工作示例 – user1441917