1

I have a query that I call with:

$query = "select * from myTable";
$results = DB::connection('myDB')->select($query);

I want to be able to get this in the simple json format of:

[{firstColumn: firstColumnValue, secondColumn: secondColumnValue},
{firstColumn: firstColumnValue, secondColumn: secondColumnValue}]

What is the easiest way to achieve this?

KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • the `$results` variable is actually an `StdClass`. You can directly use the `Response::json` facade as well if you need to directly send the data as a response. – psiyumm Nov 15 '15 at 06:37

2 Answers2

4

As I have read here : Laravel, converting data from raw query to JSON you can just use the json_encode. Hope that one helps.

Community
  • 1
  • 1
1

If you thought of writing web service or something related to it, then

$Response = array('success' => '1', 'result' => $yourData);
else
$Response = array('success' => '0', 'error' => 'Your Custom Errors');
return json_encode($Response);

So that you can handle it in while you retrieve according to the result.

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63