Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4.1 API Pagination - Create a custom JSON response

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.

My query is:

return Product::paginate(100);

My result is:

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

What I would like the result to be would be something like this:

[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?

like image 294
pkracer Avatar asked Dec 15 '22 22:12

pkracer


1 Answers

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);

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.

Hope it helps.

like image 174
ollieread Avatar answered Jan 11 '23 19:01

ollieread