Laravel 4.1 API分页-创建自定义JSON响应 [英] Laravel 4.1 API Pagination - Create a custom JSON response

查看:258
本文介绍了Laravel 4.1 API分页-创建自定义JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过简单的查询在分页结果中自定义返回对象的顺序.现在的响应顺序是分页信息,后跟数据"对象,该对象包含我搜索的模型.我想知道是否有可能颠倒订单,因此它的产品信息后跟某种分页对象.

I am wondering if it is possible to customize the order of returned objects in a paginated result from a simple query. The response order now is pagination info followed by the "data" object which contains the model I have searched for. I was wondering if its possible to reverse the order so its product info followed by some sort of pagination object.

我的查询是:

return Product::paginate(100);

我的结果是:

{"total":466,"per_page":100,"current_page":1,"last_page":5,"from":1,"to":100,"data":[{"id":28,"account_id":5,"created_by_id":14,"type":"accessory","title":"Product #28","description":"D","max_per_order":1,"stock":3565,"created_at":"1998-04-18 00:00:00","updated_at":"1994-10-28 00:00:00","deleted_at":null},{"id":44,"account_id":5,"created_by_id":36,"type":"registration","title":"Product #44","description":"Z","max_per_order":10,"stock":13739,"created_at":"2014-04-20 00:00:00","updated_at":"1991-06-03 00:00:00","deleted_at":null}]}

我希望结果是这样的:

[products: [{"id":28,"account_id":5,"created_by_id":14,"type":"accessory","title":"Product #28","description":"D","max_per_order":1,"stock":3565,"created_at":"1998-04-18 00:00:00","updated_at":"1994-10-28 00:00:00","deleted_at":null},{"id":44,"account_id":5,"created_by_id":36,"type":"registration","title":"Product #44","description":"Z","max_per_order":10,"stock":13739,"created_at":"2014-04-20 00:00:00","updated_at":"1991-06-03 00:00:00","deleted_at":null},{"id":57,"account_id":5,"created_by_id":40,"type":"accessory","title":"Product #57","description":"Z","max_per_order":4,"stock":19376,"created_at":"1970-03-27 00:00:00","updated_at":"2001-01-01 00:00:00","deleted_at":null}], pagination: {"total":466,"per_page":100,"current_page":1,"last_page":5,"from":1,"to":100}]

我将如何做这样的事情?编写自己的分页代码会更容易吗?

How would I go about doing something like this? Would it be easier to write my own pagination code?

推荐答案

无法使用laravel特有的措施来做到这一点,它需要您实际获取数据并创建自己的自定义数组以JSON形式返回.但是,这很简单,我在下面添加了一个示例.

There's no way as such using laravel specific measure to do this, it'll require you actually grabbing the data and creating your own custom array to be returned as JSON. It is however, pretty easy, I've added an example below.

$products = Product::paginate(100);
$response = [
    'products'   => $products->getItems()->toArray(),
    'pagination' => [
        'total'        => $products->getTotal(),
        'per_page'     => $products->getPerPage(),
        'current_page' => $products->getCurrentPage(),
        'last_page'    => $products->getLastPage(),
        'from'         => $products->getFrom(),
        'to'           => $products->getTo()
    ]
];

return Response::json($response);

我最近使用laravel创建了多个API,我面临的问题之一是在保持统一响应的同时对数据进行分页,这就是我的处理方式.

I've recently created several APIs with laravel, and one of the problems I faced was paginating data while keeping a uniform response, this is how I handled it.

希望有帮助.

这篇关于Laravel 4.1 API分页-创建自定义JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆