title: Laravel 4 - decoding json into view
body: <p>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.</p>
  
  <p>I have a database containing 3 rows with 4 cols. For example, lets say the columns are:</p>
  
  <p>content_id,
  content_type,
  content_data,
  timestamp</p>
  
  <p>The <strong>'content_data'</strong> column contains a JSON encoded array of around 4 key value pairs.</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>So far I've tried the following in my class:</p>
  
  <pre><code>&lt;?php
  class PageController extends \BaseController {
  
  public function index()
  {
  
      $data = Content::
          -&gt;where('content_type', 1)
          -&gt;get();
  
      foreach($data as $content)
      {
          $items[] = json_decode($content-&gt;content_data);
      }
  
      return View::make('pages.page2')
          -&gt;with('data', $data)
          -&gt;with('items', $items);
  }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>My blade template looks like this:</p>
  
  <pre><code>@extends('layouts.pages');
  
  @section('content_body')
  &lt;h1&gt;My &lt;span&gt;title&lt;/span&gt;&lt;/h1&gt;
  
  &lt;div class="column col-1"&gt;
      &lt;ul&gt;
          {{-- loop through the database rows --}}
          @foreach($data as $row)
  
              {{-- loop through the json decoded values --}}
              @foreach($items as $item)
  
                  {{ $item['title'] }}
  
              @endforeach
  
          @endforeach
      &lt;/ul&gt;
  &lt;/div&gt;
  @stop
  </code></pre>
  
  <p>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</p>
  
  <p>I hope this makes sense to someone who can help.</p>