Home » Php » Laravel, converting data from raw query to JSON [duplicate]

Laravel, converting data from raw query to JSON [duplicate]

Posted by: admin November 30, 2017 Leave a comment

Questions:

This question already has an answer here:

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));

Questions:
Answers:

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));
}