操纵雄辩的查询laravel 5.4的结果

时间:2017-08-15 17:01:03

标签: php laravel-5.4

我想从雄辩的结果中得到一个自定义的json响应。所以基本上我有一个表列出所有这样的字段:

userid
name
address1
address2
state
city
zip

然后使用这样的查询:

$results = User::paginate(10);

然后我希望得到$ results的自定义响应:

[
    {
        "id":1,
        "name": "John Doe",
        "AddressInfo" {
            "address1" : "some address1 here...",
            "address2" : "some address2 here...",
            "state" : "TX",
            "city" : "San Antonio",
            "zip" : "12345" 
        }
    },
    {
        //other records...
    }
]

我目前的解决方案是做foreach()

foreach($result as $k=>$v){
    $arrItem = [
        'id' => $v->userid,
        'AddressInfo' => [ ... ]
    ]
}

上面的代码正在运行,但在laravel中使用不同的方法和优化,例如map()。

任何人都知道如何实现输出。

由于

0 个答案:

没有答案
相关问题