hahahaha
hahahaha

Reputation: 1031

How to create nested json with laravel or mysql?

I need to have something like this:

  "results":[  
  {  
     "id":1,
     "date":{  
        "date":"2015-04-1 00:00:00.000000",
        "timezone_type":3,
        "timezone":"America\/Denver"
     }
  },

query:

$find = DB::select( DB::raw("SELECT created_at as date, 'America/Denver' as timezone, 3 as timezone_type FROM table"));
return Response::json($find);

How to create date within date with mysql/Laravel? I tried to use array_merge but date will be appended at the bottom instead of make itself nested.

Upvotes: 2

Views: 2708

Answers (1)

Arlind Hajredinaj
Arlind Hajredinaj

Reputation: 8519

Just change your code to the code below:

        $tableIds = DB::select( DB::raw("SELECT id FROM table"));
        $jsonResult = array();

        for($i = 0;$i < count($tableIds);$i++)
        {
            $jsonResult[$i]["id"] = $tableIds[$i]->id;
            $id = $tableIds[$i]->id;
            $jsonResult[$i]["date"] = DB::select( DB::raw("SELECT created_at as date, 'America/Denver' as timezone, 3 as timezone_type  FROM table WHERE id = $id"));
        }

        return Response::json(array(
                    'error'     =>  false,
                    'stores'    =>  $jsonResult),
                    200
            );

Upvotes: 3

Related Questions