在laravel中传递json对象中数据库的数据

时间:2017-03-14 07:13:37

标签: json laravel

嗯,这就是我所做的:

     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"}]}

我该怎么做?

3 个答案:

答案 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);
}