Search code examples
phplaravel-5

Laravel 5.4 Getting issue in fetching from databse in Json


I have been fetching data from database and in return json I got this format. As you see, I need to remove extra brackets and "0".

This is the code:

$showid=User::create($request->all());
return str_replace(array('[', ']'), '', htmlspecialchars(json_encode(['statusCode'=>'0','statusMessage'=>'Success',$showid]), ENT_NOQUOTES));

Return output:

{
    "0": {
        "Email": "fghgfkfakgf@gmail.com",
        "Password": "hahah",
        "Role": "2",
        "updated_at": "2017-04-19 10:59:54",
        "created_at": "2017-04-19 10:59:54",
        "id": 135
    },
    "statusCode": "0",
    "statusMessage": "Success"
}

And I want to output like this:

{
    "statusCode": "0",
    "statusMessage": "Success",
    "Email": "fghgfkfakgf@gmail.com",
    "Password": "hahah",
    "Role": "2",
    "updated_at": "2017-04-19 10:59:54",
    "created_at": "2017-04-19 10:59:54",
    "id": 135
}

Check the following image.

Jason Return Output


Solution

  • try this:

    $showid=User::create($request->all()); 
    $rs = [
            'statusCode' => '0',
            'statusMessage' => 'Success'
        ];
    
    return response()->json(array_merge($rs, $showid));