Laravel 4 - decoding json into view

17,954

Use an Eloquent Accessor to transform your data as you need it.

/**
 * Eloquent accessor to transform our content_data column
 * from JSON to an array.
 * 
 * @param  string  $data  Original JSON data
 * @return array
 */
public function getContentDataAttribute($data)
{
    return json_decode($data);
}

You can then retrieve your column via $content->content_data as usual, but it will be converted into an array.

You can do the inverse when saving data to that column by transforming the passed array into a JSON encoded string, also, using a mutator.

Share:
17,954
Goonyan Harir
Author by

Goonyan Harir

Updated on June 06, 2022

Comments

  • Goonyan Harir
    Goonyan Harir almost 2 years

    I've been recently experimenting with Laravel 4, which so far has been a joy to use. However, I am experiencing an issue, not with Laravel 4, but rather my own inabilities.

    I have a database containing 3 rows with 4 cols. For example, lets say the columns are:

    content_id, content_type, content_data, timestamp

    The 'content_data' column contains a JSON encoded array of around 4 key value pairs.

    When I retrieve these rows from the database (using Eloquent) and pass the data into my view, how can I also parse the JSON into my blade template?

    After searching and referring to the Laravel documentation my thought is that you can't, so I tried to decode the JSON back into an array within my controller and then pass it into my view.

    So far I've tried the following in my class:

    <?php
    class PageController extends \BaseController {
    
    public function index()
    {
    
        $data = Content::
            ->where('content_type', 1)
            ->get();
    
        foreach($data as $content)
        {
            $items[] = json_decode($content->content_data);
        }
    
        return View::make('pages.page2')
            ->with('data', $data)
            ->with('items', $items);
    }
    }
    

    However in my Blade template, when I run a foreach loop to loop through the extracted rows, I have tried running another foreach loop within the first one which loops through the $items array to to extract their values, but because it's a loop within a loop, I get duplicate json values.

    My blade template looks like this:

    @extends('layouts.pages');
    
    @section('content_body')
    <h1>My <span>title</span></h1>
    
    <div class="column col-1">
        <ul>
            {{-- loop through the database rows --}}
            @foreach($data as $row)
    
                {{-- loop through the json decoded values --}}
                @foreach($items as $item)
    
                    {{ $item['title'] }}
    
                @endforeach
    
            @endforeach
        </ul>
    </div>
    @stop
    

    Not sure if I've explained it correctly, but basically I just want to be able to parse my json encoded array within the loop that displays the extracted db rows

    I hope this makes sense to someone who can help.

  • Goonyan Harir
    Goonyan Harir over 10 years
    It worked perfectly, although it did take me a while to get my head around the accessor's naming convention and how to access my array from within Blade, but I managed to figure it out. There is a small typo in your code btw, the return should be json_encode.Thank you very much to all of the contributors.