我正在使用laravel
中的联接查询从数据库中获取数据并传递给json
,并在数组中获得一些结果,但是我想要下面给出的对象
控制器代码:
$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]);
上面的代码将给出以下数组中的json实际上我想要对象中的json
{
"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"
},
}
}
答案 0 :(得分:0)
尝试一下,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]);
答案 1 :(得分:0)
您想要的不是有效的JSON,您可以在此处进行检查:https://jsonformatter.curiousconcept.com/
因此,您无法创建这样的输出,并且如果强制执行该输出,则将收到该输出的应用程序将无法正确解析它,因此这没有任何意义。