嗯,这就是我所做的:
public function api_index()
{
$agents=DB::table('users')
->select('id','username','role')
->get();
return response()->json($agents);
}
它在浏览器中抛出它:
[{"id":"1","username":"suzan","role":"Admin"}, {"id":"2","username":"momta","role":"admin"}]
但我实际上需要的是我需要一个json对象,在那个json对象上我需要这些数据。可能是这样的:
{"jsonboject":[{"id":"1","username":"suzan","role":"Admin"},{"id":"2","username":"momta","role":"admin"}]}
我该怎么做?
答案 0 :(得分:2)
试试这个
public function api_index()
{
$agents=DB::table('users')
->select('id','username','role')
->get();
return response(['jsonboject' => $agents]);
}
答案 1 :(得分:2)
这样做:
return response()->json(['jsonobject' => $agents]);
答案 2 :(得分:1)
执行以下代码:
public function api_index()
{
$agents=DB::table('users')
->select('id','username','role')
->get();
return response()->json(array('jsonobject'=>$agents),200);
}