Laravel, converting data from a raw request to JSON

Hy all, can anyone help me with converting some data that will be returned from the model (based on RAW request) to 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 the controller?

Here is the request:

 $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; 
+4
source share
2 answers

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)); 
+8
source

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)); } 
+2
source

Source: https://habr.com/ru/post/1481651/


All Articles