Laravel 4 - decoding json into view

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:

class PageController extends \BaseController {

public function index()

    $data = Content::
        ->where('content_type', 1)

    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:


<h1>My <span>title</span></h1>

<div class="column col-1">
        {{-- loop through the database rows --}}
        @foreach($data as $row)

            {{-- loop through the json decoded values --}}
            @foreach($items as $item)

                {{ $item['title'] }}



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.


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.

Need Your Help

PHP date dd-mm-yyyy

php date zend-form

I would like to fill a date in as dd-mm-yyyy but I would like to save the date as Y-m-d in my database. What I have is this:

Express Error: EMFILE, too many open files

node.js express pug

I am getting the following very annoying error: