2016-07-30 82 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