1

I've some JSON data stored in a field of my DB, to display the data in a view, I wrote this query in my controller

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

In the controller, I pass that query, and other queries, to a view in this way

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

in the sidebar section of the view I used this code:

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

Well, as a result, when I try to load the page, I don't see any code under the <ul class="gallery">.

The structure of the JSON data is this http://p4c.it/alfa.json

Considering that json_decode needs a string, in my opinion there is something wrong in the controller part. Do you have any suggestion?

3
  • 1
    have you tried like this <li>{{ $galleria->cover_gal }}</li> Commented Jul 30, 2016 at 15:15
  • cover_gal is part of the JSON data. The structure of the JSON data is this p4c.it/alfa.json Commented Jul 30, 2016 at 15:23
  • First check the query output, I think your query is wrong, in the 'like' clause you have specified the column name(%nome_gal%), Commented Jul 30, 2016 at 15:33

3 Answers 3

1

Looks like your JSON data has certain issue: If you considering 'id_foto' & 'nome_foto' a pair then it's should be

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

instead of

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

because when we json_decode() your json data we get only the last 'id_foto' & 'nome_foto' like

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

which I guess is wrong and check out the edited View code

Controller

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

View [EDITED]

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

And to access the keys nome_gal & cover_gal

$gallerie['nome_gal']
$gallerie['cover_gal']
Sign up to request clarification or add additional context in comments.

2 Comments

the entire page is white :( I suspected that the JSON data wasn't valid, but, when I try to load it in a test route everything works fine $gallerie = \App\Articolo::select('nome_gal', 'id_articolo')->where('nome_gal','LIKE','%nome_gal%')->orderBy('created_at', 'desc')->orderBy('updated_at', 'desc')->take(34)->get(); foreach ($gallerie as $galleria) { $galla=json_decode($galleria['nome_gal'], true); echo '<p>'.$galleria->id_articolo.' - '.$galla['cover_gal'].' - '.$galla['nome_gal'].'</p>'; }
looks like your json data has certain issue specially in galleria.
0

Your json has same key name inside galleria array so it will take the last value only. instead you can use as

{
    "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"
    }]
}

Here is your json decoded array

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

        )

)

1 Comment

infact it was one of the problems that I've corrected p4c.it/gamma.json . Anyway, I'm trying to access cover_gal not galleria. I also tried to simplify my json eliminating the array part but without success. In this moment I'm looking for a working example of json passed to a view
0

Well this is what i did and it worked for me, in the following, everything is recap, so the solution is complete for the new readers. In the model of the table I defined an imaginary attribute like so:

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

and obviously

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

in the controller I've the query:

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

and obviously the passage to the view:

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

finally, in the view, I solved in this way:

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

for some reason the regular syntax

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

didn't worked for me (white page of death as a result).

Thanks to everyone for the contribution, I know php but I'm new to laravel :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.