0

I am getting data from database using join query in laravel & pass to json and getting some result in array but I want object that I given below

Controller code:

$resultPastActivity= DB::table('table_user_create_activity')
                            ->join('table_sub_category','table_user_create_activity.selected_activity_id', '=', 'table_sub_category.sub_category_id')
                            ->select('sub_category_name','area','activity_type','activity_date','start_time','end_time')
                            ->whereDate('activity_date', '<', $todayDate)
                            ->where('user_id',$user_id)
                            ->get();

 return response()->json(['success' => '1','data' =>$resultPastActivity]);

The above code will give following json that is in array actually i want json in object

{
    "success": "1",
    "data": [
        {
            "sub_category_name": "Badminton",
            "area": "Rankala lake",
            "activity_type": "1",
            "activity_date": "2018-01-12",
            "start_time": "15:04:49",
            "end_time": "20:05:69"
        },
        {
            "sub_category_name": "Football",
            "area": "Devakar panad",
            "activity_type": "1",
            "activity_date": "2018-01-15",
            "start_time": "15:04:49",
            "end_time": "20:05:69"
        },
    ]
}



i want json as follows
{
    "success": "1",
    "data": {
        {
            "sub_category_name": "Badminton",
            "area": "Rankala lake",
            "activity_type": "1",
            "activity_date": "2018-01-12",
            "start_time": "15:04:49",
            "end_time": "20:05:69"
        },
        {
            "sub_category_name": "Football",
            "area": "Devakar panad",
            "activity_type": "1",
            "activity_date": "2018-01-15",
            "start_time": "15:04:49",
            "end_time": "20:05:69"
        },
    }
}
5
  • You tried flatten()? Thats an method for Collections. Commented May 23, 2019 at 12:07
  • quick way to do this $obj = json_decode(json_encode($array)); Commented May 23, 2019 at 12:12
  • Possible duplicate of how to convert multidimensional array to object in php? Commented May 23, 2019 at 12:13
  • i tried but get same json with array? suggest another method Commented May 23, 2019 at 12:20
  • That is not valid json, to have multiple objects in a structure you need to wrap it in an array, you want an object of multiple objects which is not possible Commented May 23, 2019 at 12:28

2 Answers 2

0

try this, json_encode($json, JSON_FORCE_OBJECT) JSON_FORCE_OBJECT

$result=array();
$result["0"]=$resultPastActivity;
$json=json_encode((object)$result,JSON_FORCE_OBJECT);
return response()->json(['success' => '1','data' =>$json]);
Sign up to request clarification or add additional context in comments.

3 Comments

i tried but can not get expected result plz need another solutions
yes This is result contain "\0" that is not expected { "success": "1", "data": "{\"0\":{\"0\":{\"sub_category_name\":\"Badminton\",\"area\":\"Rankala lake\",\"activity_type\":\"1\",\"activity_date\":\"2018-01-12\",\"start_time\":\"15:04:49\",\"end_time\":\"20:05:69\"},\"1\":{\"sub_category_name\":\"Football\",\"area\":\"Devakar panad\",\"activity_type\":\"1\",\"activity_date\":\"2018-01-15\",\"start_time\":\"15:04:49\",\"end_time\":\"20:05:69\"},}}}"
Please show the updated answer add $json=json_encode((object)$result,JSON_FORCE_OBJECT); type cast $result with (object) @smitapatil
0

The one you want is not a valid JSON, you can check it here: https://jsonformatter.curiousconcept.com/

So, you cannot create such an output, and, if you force it, the application that will receive it won't parse it correctly, so it doesn't make sense.

Comments

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.