4

Hy all, can anyone help me with converting some data which will be return from model(based on the RAW query) into JSON.

So in my controller i have something like:

public function get_index() {
    $data = Something::getDataFromRawQuery();

    return View::make('....')->with('data', $data);
}

So my question is how to forward JSON data to the view from controller?

Here is the query:

$apps = DB::query('SELECT a.name,
    a.desc,
    a.sig,
    ar.rate
    FROM something a
    INNER JOIN something_else ar
    ON (a.id=ar.something_id)
    ORDER BY ar.rate DESC'
 );

 return $apps;
Srle
  • 10,366
  • 8
  • 34
  • 63

2 Answers2

8

DB::query returns a simple array, so just call json_encode directly on it:

$data = Something::getDataFromRawQuery();

return View::make('....')->with('data', json_encode($data));
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
3

Just use json_encode()

public function get_index() {
    $data = Something::getDataFromRawQuery();

    /* Do your loop here to build an array "results" from $data, if necessary
       Really depends on what ::getDataFromRawQuery returns. */

    return View::make('....')->with('data', json_encode($results));
}
Gary Green
  • 22,045
  • 6
  • 49
  • 75