Matt Aebersold
Matt Aebersold

Reputation: 1

Laravel 4.2 JSON response not creating formatted JSON

I'm creating a JSON response with data from a Laravel 4.2 install. This is a simple loop through a model, and pushing data to an array. Then, I'm encoding that array, and attempting to return JSON to a route (/feed for example).

What is returned isn't auto formatted by Chrome or Firefox, so that leads me to believe that I'm not forming the JSON correctly.

See this as my example:

public function feed() {
    $feed = CalendarEvent::ordered()->visible()->get();
    $events = array();

    foreach($feed as $item) {

      $event = array(
        'event' => array(
          'id' => $item->id,
          'title' => $item->title,
          'date' => $date
        )
      );

      array_push($events, $event);

    }

    $json = json_encode(array("events"=>$events));
    return Response::json($json);

  }

It is formatted like this in Chrome/FF example of rendered JSON in the browser

Upvotes: 0

Views: 245

Answers (1)

khartnett
khartnett

Reputation: 861

Response::json() expects the un-encoded json array, just remove the json_encode line and:

return Response::json(array("events"=>$events));

Upvotes: 2

Related Questions