2

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.

1
  • Show us how your table in mysql looks like and the code that you use to get the array from datatabase? Commented Apr 1, 2015 at 10:11

1 Answer 1

3

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
            );
Sign up to request clarification or add additional context in comments.

2 Comments

You could use a recursive function that iterates through each level, what do you have in mind?
Great coding but faced problem, I am creating nested json response in laravel. I am taking three tables as category_type={category_type_id,category_name}; main_category={main_category_id,category_type_id,main_category_name} sub_category={sub_category_id,category_type_id,main_category_id,sub_category_name} and I am using coding and getting json but I want to nested third table in second plZ changes in coding I already nested second in first now I want nested third table in second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.