Laravel 5.4从Json中的数据库中获取问题

时间:2017-04-19 11:03:34

标签: laravel-5

我一直在从数据库中获取数据,作为回报,json我得到了这种格式。 如您所见,我需要删除额外的括号和“0”。

这是代码:

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

返回输出:

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

我想这样输出:

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

检查以下图像。

Jason Return Output

3 个答案:

答案 0 :(得分:1)

试试这个:

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

return response()->json(array_merge($rs, $showid));

答案 1 :(得分:0)

echo str_replace(array('[',']'),'',htmlspecialchars(json_encode(['statusCode'=>'0','statusMessage'=>'成功',$ showid]), ENT_NOQUOTES));

答案 2 :(得分:0)

返回str_replace(数组(' [',']'),'',htmlspecialchars(json_encode(array([' statusCode& #39; =>' 0',' statusMessage' =>'成功',$ showid]),ENT_NOQUOTES)));