Wednesday, March 15, 2017

Passing the data of database in json object in laravel

Well, here is what I did:

     public function api_index()
{

    $agents=DB::table('users')
        ->select('id','username','role')
        ->get();

    return response()->json($agents);
}

And its throwing this in browser:

     [{"id":"1","username":"suzan","role":"Admin"},       {"id":"2","username":"momta","role":"admin"}]

but i actually needed is i need a json object and on that json object i need these data. may be like this:

    {"jsonboject":[{"id":"1","username":"suzan","role":"Admin"},{"id":"2","username":"momta","role":"admin"}]}

How can i do this?



via Suz Aann shrestha

Advertisement