为什么json在eloquent中多次显示数组索引和数组对象

时间:2016-02-12 06:42:28

标签: php eloquent

我的JSON输出如下:

{
    "0": "1",
    "1": "araer",
    "2": "aeraer",
    "3": "aeraer",
    "4": "News/Magzine Website",
    "5": "2016-01-22 13:15:56",
    "6": "2016-01-22 13:15:56",
    "id": "1",
    "name": "araer",
    "email": "aeraer",
    "url": "aeraer",
    "web": "News/Magzine Website",
    "created_at": "2016-01-22 13:15:56",
    "updated_at": "2016-01-22 13:15:56"
}, {
    "0": "2",
    "1": "asd",
    "2": "asd",
    "3": "sfd",
    "4": "sdf",
    "5": "2016-02-10 13:06:28",
    "6": "0000-00-00 00:00:00",
    "id": "2",
    "name": "asd",
    "email": "asd",
    "url": "sfd",
    "web": "sdf",
    "created_at": "2016-02-10 13:06:28",
    "updated_at": "-0001-11-30 00:00:00"
}

我在模型中使用的代码:

public function getBlog() {
    try {
        $result = $this - > get();
        return $result;
    } catch (Exception $ex) {
        return array();
    }
    return array();
}

2 个答案:

答案 0 :(得分:1)

检查config / databse.php数组中的PDO获取样式。如果它不存在那么它将使用PDO :: FETCH_BOTH(默认情况下),它返回关联和数值。要仅获取关联值,您需要设置'fetch' => PDO::FETCH_CLASS,'fetch' => PDO::FETCH_ASSOC

答案 1 :(得分:0)

您的功能将从博客表中重新输入所有记录。 如果你想获得单一记录,那么试试

$this->get()->first();